如何在 jQuery 触发器(“单击”)上运行回调函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15519410/
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
How to run a callback function on a jQuery trigger("click")?
提问by frequent
I need to trigger a custom event in the callback of a trigger
call, but I can't get it to work.
我需要在trigger
调用的回调中触发一个自定义事件,但我无法让它工作。
I tried this:
我试过这个:
var $input = $( ".ui-popup-container" ).find( "input" ).eq(2);
function runtests () {
console.log("clicked the input");
};
$input.trigger('click', runtests());
and this:
和这个:
var $input = $( ".ui-popup-container" ).find( "input" ).eq(2);
$input.trigger('click', function(){
console.log("clicked the input");
}
Neither of which works.
两者都不起作用。
Question:
How do I get a callback function to run when I'm triggering a click on an element?
问题:
如何在触发元素上的点击时运行回调函数?
采纳答案by Denys Séguret
When you call trigger
, the bound event handler is immediately executed, so you don't need any callback. Just use
当您调用 时trigger
,绑定的事件处理程序会立即执行,因此您不需要任何回调。只需使用
$input.trigger('click');
runtests();
回答by Naren Chejara
Yes - It is true that trigger doesn't take callback but we can pass callback as parameter.
是的 - 触发器确实不接受回调,但我们可以将回调作为参数传递。
//.trigger( eventType [, extraParameters ] )
$("#element").bind("customCall", function(e, callback){
callback();
}
var callback = function(){alert("Hello");}
$("#element").trigger("customCall",[callback]);
Hope this will helps
希望这会有所帮助
回答by Mahesh Sapkal
First you need to bind the click event and then you can trigger the click event.
首先需要绑定click事件,然后才能触发click事件。
$input.bind('click', function() {
console.log("clicked the input");
});
$input.trigger('click');
回答by Dipesh Parmar
Trigger does not have callback function.
触发器没有回调函数。
.trigger( eventType [, extraParameters ] )
What you can do
你可以做什么
var $input = $( ".ui-popup-container" ).find( "input" ).eq(2);
$input.on('click',function(){
console.log("clicked the input");
});
回答by FVlad
You need to use bind
or on
to add callbacks. In your case it should look like this:
您需要使用bind
或on
添加回调。在您的情况下,它应该如下所示:
var $input = $( ".ui-popup-container" ).find( "input" ).eq(2);
function runtests () {
console.log("clicked the input");
};
$input.bind('click', runtests);
Even shorter version for binding click is $input.click(runtests)
绑定点击的更短版本是 $input.click(runtests)
Then it will be called on click or you can trigger it manually with $input.trigger('click')
or simply $input.click()
.
然后它将在单击时调用,或者您可以使用$input.trigger('click')
或 简单地手动触发它$input.click()
。
回答by MarcoK
回答by realplay
.trigger()
does not accept any callbacks. You don't need it most cases because the bound event handler is immediately executed. However, if you have an AJAX call in trigger event handler, the code from accepted answer will not work:
.trigger()
不接受任何回调。大多数情况下您不需要它,因为绑定的事件处理程序会立即执行。但是,如果您在触发器事件处理程序中有 AJAX 调用,则接受的答案中的代码将不起作用:
$input.trigger('click');
runtests();
You will have to use setTimeout()
like this:
你将不得不setTimeout()
像这样使用:
$input.trigger('click');
setTimeout(function () {
runtests();
}, 250);
Or consider using success
or error
callbacks for .ajax()
call in .trigger()
event handler.
或者考虑使用success
或error
回调来.ajax()
调用.trigger()
事件处理程序。
回答by Simon
If you have a click handler on every input but want to do something on a specific element:
如果您对每个输入都有一个点击处理程序,但想对特定元素执行某些操作:
var $container = $(".ui-popup-container"),
special = 2;
$container.on('click', 'input', function() {
// do something for every input
if($(this).index() == special) {
// your "callback", e.g. the function you want to execute on input:eq2 click
myfunction();
}
}).find('input').eq(special).trigger('click');