Binding multiple events in jQuery

后端 未结 2 1953
清酒与你
清酒与你 2021-02-20 01:24

I have a custom jQuery plugin which binds a change event to a form element, in this case some input elements. The change event is used to

相关标签:
2条回答
  • 2021-02-20 01:43

    Events stack in jQuery. If you do:

    <input type="button" value="Press me">
    

    and:

    $(":button").click(function() {
      alert("click");
    }).click(function() {
      alert("click 2");
    });
    

    both will fire. So just bind another event to the relevant elements. It won't overwrite the first.

    0 讨论(0)
  • 2021-02-20 01:47

    You can bind multiple handlers for the same event. Bind the second change as you normally would:

    ​<input type="text" id="a" />​​​​​​​​​​​​​​​
    
    ​$("#a"​).change(function() { alert("1"); });
    $("#a").change(function() { alert("2"); });​
    
    $("#a").change(); // alerts 1, then 2
    
    0 讨论(0)
提交回复
热议问题