jQuery keyup 功能未执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14416517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery keyup function not executing
提问by omerjerk
I have this code :
我有这个代码:
<script>
$('#searchInput').keyup(function() {
alert('Handler for .keyup() called.');
});
</script>
and this input :
这个输入:
<input id='searchInput' type='text' placeholder='Search other alumni users' />
but when I press a key, the alert doesn't come up... I've included the jQuery script already.
但是当我按下一个键时,警报没有出现……我已经包含了 jQuery 脚本。
回答by Denys Séguret
Change your code to
将您的代码更改为
$(function(){ // this will be called when the DOM is ready
$('#searchInput').keyup(function() {
alert('Handler for .keyup() called.');
});
});
to ensure the DOM element exists when you build the jQuery set.
以确保在构建 jQuery 集时存在 DOM 元素。
回答by Anujith
$(document).ready(function() {
$('#searchInput').keyup(function() {
alert('Handler for .keyup() called.');
});
});
Or
或者
$(function() {
$('#searchInput').keyup(function() {
alert('Handler for .keyup() called.');
});
});
回答by Josh Thorne
keyup
/keydown
seem to only work on elements that are present at document.ready
.
keyup
/keydown
似乎只适用于存在于document.ready
.
If you are triggering your event from elements that were altered or injected post pageload, these events will not fire.
如果您从页面加载后更改或注入的元素触发事件,则不会触发这些事件。
Finding or creating a parent of the element to be watched that's present on load then setting the event to trigger from that parent element can bypass this.
查找或创建加载时要监视的元素的父元素,然后将事件设置为从该父元素触发可以绕过这个。
回答by Anthony Griggs
In my case I had multiple functions within the $(document).ready(function () {});
就我而言,我在 $(document).ready(function () {});
By changing the order of my Keyup functions i.e. placing them at the top... within document ready seemed to do the trick. My guess is that one of the other functions was breaking they keyup function for what ever reason?
通过更改我的 Keyup 函数的顺序,即将它们放在顶部......在文档中准备好似乎可以解决问题。我的猜测是其他功能之一因为什么原因破坏了它们的 keyup 功能?
回答by Alex Williams
This worked for me:
这对我有用:
jQuery(".divForinput1 > input").keyup(function() {
var value = $( this ).val();
jQuery( ".divForinput2 > input" ).val( value );
}).keyup();
//the .keyup on the end maybe what many are missing