jQuery jquery更改事件回调
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15805000/
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 change event callback
提问by Alireza41
How to call a function once after change()
event complete?
change()
事件完成后如何调用一次函数?
for example, something like this: ( I know jQuery Doesn't have callback method as default )
例如,这样的事情:(我知道 jQuery 没有默认回调方法)
$('#element').change( function(){
// do something on change
// $('#milestonesSelect').multiselect({ minWidth: 120 , height : '200px' ,selectedList: 4 }).multiselectfilter();
// some animation calls ...
// ...
}, function(){
// do something after complete
alert('another codes has completed when i called');
}
);
Is it possible to call a single callback after all the codes on change method done, except call a complete callback for every functions on it?
是否可以在更改方法的所有代码完成后调用单个回调,除了为其上的每个函数调用一个完整的回调?
I need to do something after the change event has completed
更改事件完成后我需要做一些事情
Shall I need to set order to methods in change handler?
我是否需要为更改处理程序中的方法设置顺序?
回答by Arun P Johny
You can probably make use of event bubblingand register a callback in the document
.
您可能可以利用事件冒泡并在document
.
$(document).on('change', '#element', function(){
console.log('after all callbacks')
});
Demo: Fiddle
演示:小提琴
回答by David Kartik
I just spent some time exploring this myself, and decided to submit my answer to this question as well. This is not utilizing any of jQuery's deferred methods, which might be a better approach. I haven't explored them enough to have an opinion on the matter.
我只是花了一些时间自己探索这个问题,并决定也提交我对这个问题的回答。这没有使用任何 jQuery 的延迟方法,这可能是更好的方法。我还没有对它们进行足够的探索,无法对此事发表意见。
$("#element").change(function() {
handler().after(callBack($("#element").val()));
}
function handler() {
alert("Event handler");
}
function callBack(foo) {
alert(foo);
}
Demo: JSFiddle
演示:JSFiddle
回答by chrx
If I understand your question correctly, this will execute code only one time after a change event is fired:
如果我正确理解您的问题,这将仅在触发更改事件后执行一次代码:
var changed = false;
$('#element').on('change', function() {
if ( !changed ) {
// do something on change
changed = true;
}
}
});