简体   繁体   English

多按钮单击事件Jquery On

[英]Multiple Button Click Events Jquery On

我可以将多个Button Click事件绑定到以下相同功能吗?

$(document).on("click", "#btn1 #btn2 #btn3", callbackFunction);

Use commas to separate the values: 使用逗号分隔值:

$(document).on("click", "#btn1, #btn2, #btn3", callbackFunction);

Then you can determine which one called you by accessing the this object, so the following would alert the id of the element clicked: 然后,您可以通过访问this对象来确定哪个调用了您,因此以下内容将提醒所单击元素的ID:

$(document).on("click", "#btn1, #btn2, #btn3", function () { 
     alert($(this).attr("id"));
});

For example, this fiddle: http://jsfiddle.net/SFLDw/1/ 例如,这个小提琴: http : //jsfiddle.net/SFLDw/1/

Yes you can. 是的你可以。 However, you need to separate them with a comma. 但是,您需要用逗号分隔它们。

$(document).on("click", "#btn1, #btn2, #btn3", callBackFunction); $(document).on(“ click”,“#btn1,#btn2,#btn3”,callBackFunction);

If possible, it might be wise to assign a class to the buttons and use the class as the seletor. 如果可能的话,为按钮分配一个类并将其用作选择器可能是明智的。 Any button with that class will be bound to the event. 具有该类的任何按钮都将绑定到该事件。 This will keep you from having to add additional selectors. 这将使您不必添加其他选择器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM