jquery change event callback

走远了吗. 提交于 2019-12-09 10:40:00

问题


How to call a function once after change() event complete?

for example, something like this: ( I know jQuery Doesn't have callback method as default )

$('#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?


回答1:


You can probably make use of event bubbling and register a callback in the document.

$(document).on('change', '#element', function(){
    console.log('after all callbacks')
});

Demo: Fiddle




回答2:


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.

$("#element").change(function() {
  handler().after(callBack($("#element").val()));
}

function handler() {
  alert("Event handler");
}

function callBack(foo) {
  alert(foo);
}

Demo: JSFiddle




回答3:


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;
    }
  }
});


来源:https://stackoverflow.com/questions/15805000/jquery-change-event-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!