由 ajax 调用加载的页面文本框上的 Jquery 按键事件

Jquery keypress event on textbox in page loaded by ajax call

本文关键字:Jquery 事件 调用 ajax 加载 文本      更新时间:2023-09-26

我有主页index.php,在此页面上单击"我是加载网址.php使用ajax。

在网址中.php我有text box.我希望当用户在此文本框中学习课程输入某些内容时,它显示的下方按钮变得可见。

网址.php

<input type="text" id="text" name="sent" contenteditable="true"  style=" text-align: left; height: 30px; width:512px; " placeholder="Enter URL ..."/></input>
<button id="b1" style="display:none" > Get Sentiment </button>

索引.php

在身体部位:

<script>
    $("#text").keypress( function() {
        document.getElementById("b1").style.display = "block";
        console.log( "Handler for .keypress() called." );
    });
</script>

但是当我转到文本框并单击它时,文本框不会出现。我也尝试过 blurfocus代替keypress但没有变化。

要加载 url.php使用 ajax,我有以下代码:

<input type="button" id="load_url" value="Load url.php" />
$("#load_url").click(function(){
    $.ajax({
        url:"url.php",
        success:function(response) {
            $("#view_port").html(response);
            $("#load_url").hide();
        }
    }); 
});

url.php更改为:

<textarea id="text" name="sent" contenteditable="true" style=" text-align: left; height: 30px; width:512px; " placeholder="Enter URL ..."></textarea>
<input type="button" id="b1" style="display:none" value=" Get Sentiment" />

<input type="text" id="text" name="sent" contenteditable="true" style=" text-align: left; height: 30px; width:512px; " placeholder="Enter URL ..." />
<input type="button" id="b1" style="display:none" value=" Get Sentiment" />

和使用

$(document).ready( function() {
    $("#text").bind("keypress", function( e ) {...});
});

希望它能有所帮助。

我想你忘了在 AJAX 加载后注册 keypress 事件。

    $( document ).ready( function(e) {
       $('#button').click(function(e) { // Clicking the button to load the url.php
          $('#somediv').load("url.php", function() { // loading the url.php
             $('#text').keypress(function(e) { // You are registering the keypress event listener after the ajax load
                 $('#b1').show('fast');
             });
          }
       });
   });

试试这个

<script type="text/javascript">
    $("#text").keypress( function() {
        $("#b1").show();
        console.log( "Handler for .keypress() called." );
    });
</script>