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

标签 javascript jquery ajax

我有主页 index.php 并且在这个页面上单击按钮我正在使用 ajax 加载 url.php。

在 url.php 中我有 文本框。我希望当用户在这个 texbox 中输入内容时,它下面显示的按钮变得可见。

url.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 但没有变化。

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

<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 ) {...});
});

希望对您有所帮助。

关于javascript - 由ajax调用加载的页面中文本框上的Jquery按键事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19353986/

相关文章:

javascript - 加载资源时传递cookie而不是ajax?

javascript - 使用 mongoose 和 Promise.all 更新子文档

javascript - 将多个 if-else 语句转换为对象的 switch-case

javascript - CKEditor 可以工作 - 但出现错误 "Uncaught ReferenceError: CKEDITOR is not defined"

jquery - 设计容器框下拉菜单时遇到问题

php - jquery ajax 到 mysql 到 php 到 javascript 的转换

javascript - 在值中插入特殊字符

javascript - 如何使用 JQuery 动态地将文本加载到段落中

脚本错误;未捕获的语法错误 : missing ) after argument list

jquery - console.log 在 jquery ajax 函数中不起作用