jQuery 手动触发jquery keyup?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6511846/
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
manually trigger jquery keyup?
提问by Devil's Advocate
I think I figured it out... see my comment. Sorry.
我想我想通了……看我的评论。对不起。
I have a JSON web service that is called on a certain keyup() event which retrieves some data based on the input. I would like to load an initial set of data before anything is typed. I'm trying to manually invoke the keyup() as stated in the documentation, but it doesn't seem to do anything.
我有一个 JSON Web 服务,它在某个 keyup() 事件上调用,它根据输入检索一些数据。我想在输入任何内容之前加载一组初始数据。我正在尝试按照文档中的说明手动调用 keyup(),但它似乎没有做任何事情。
$('#inputItemName').keyup(function () {
//perform query, display results
});
^^ works great when you type in the box
^^ 在框中输入时效果很好
$('#inputItemName').keyup();
^^ called immediately after document ready function, doesn't seem to do anything.
^^ 在文档就绪函数之后立即调用,似乎没有做任何事情。
回答by Radu
Works fine for me: http://jsfiddle.net/radu/gD5WL/
对我来说很好用:http: //jsfiddle.net/radu/gD5WL/
$('#test').keyup(function () {
console.log('hello');
});
$('#test').keyup();
The keyup
event is getting called on page load.
该keyup
事件在页面加载时被调用。
回答by Senad Me?kin
$('#inputItemName').trigger('keyup');
回答by Teja Kantamneni
You are missing a #
in selector...
try with hash...
您缺少一个#
in 选择器...尝试使用哈希...
$('#inputItemName').keyup();
$('#inputItemName').keyup();
回答by Devil's Advocate
I think I figured it out... I moved the function that calls the results higher up in the script tag than the keyup()
invocation and now it works.
我想我想通了……我将在脚本标记中keyup()
调用结果的函数移动到比调用更高的位置,现在它可以工作了。
回答by Piotr Perak
Try trigger function.
尝试触发功能。
$('#inputItemName').trigger('keyup');
$('#inputItemName').trigger('keyup');
回答by glortho
You're missing the hash marker for the id in the second one:
您在第二个中缺少 id 的哈希标记:
$('#inputItemName').keyup();
$('#inputItemName').keyup();
回答by richsoni
Try this:
尝试这个:
$('#inputItemName').bind('keyup',function() {
//stuff
});