由 ajax 调用加载的页面文本框上的 Jquery 按键事件
Jquery keypress event on textbox in page loaded by ajax call
我有主页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>
但是当我转到文本框并单击它时,文本框不会出现。我也尝试过 blur
和focus
代替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>
相关文章:
- 在创建对象后附加一个jquery事件
- 将jQuery事件应用于所有类元素
- Jquery事件处理程序仅适用于匿名函数
- 如何在jQuery事件处理程序中存储和重用超时
- 无线电输入更改的jQuery事件未启动
- 我应该/如何清除mousemove JQuery事件侦听器
- 是否可以在jQuery事件中更改Angular范围
- jQuery事件侦听器多次启动
- jQuery 事件处理程序堆叠
- 如何在不删除类似侦听器的情况下从父对象中删除jQuery事件侦听器
- 一个接一个地触发jQuery事件
- 在JQuery事件中查找父元素
- 堆叠jQuery事件(动画)
- 在jquery事件中引用javascript对象
- 动态添加对象的jQuery事件处理程序
- jQuery事件命名空间是否可以包含破折号
- Jquery事件绑定获胜'当作为方法调用时不起作用,但当直接在控制台中调用时会起作用
- 没有为Ajax添加的新元素注册Jquery事件
- 动态创建的DOM元素上的jQuery事件绑定
- jQuery事件未在动态生成的内容上激发