简体   繁体   English

在jquery中为单个处理程序将多个事件附加到多个元素

[英]Attaching multiple events to multiple elements for a single handler on the go in jquery

I want a click event to be attached to 'buttonID' selector and an 'onChange' event to the 'dropDownID' selector, because for both of these events I want to execute the same handler method. 我希望将click事件附加到“ buttonID”选择器,将“ onChange”事件附加到 dropDownID”选择器,因为对于这两个事件,我都希望执行相同的处理程序方法。 I tired the below code but what happens is the click and change event is bound to BOTH button and dropDown, so for selecting a value in dropDown I click it and AJAX gets triggered and agian after the dropDownBox's value is changed it'll generate another ajax call. 我厌倦了下面的代码,但是发生的是单击和更改事件绑定到BOTH按钮和dropDown上,因此,在dropDown中选择一个值时,我单击它,更改dropDownBox的值后会触发AJAX和agian,它将生成另一个ajax呼叫。 With-effect Ajax is triggered two times when I try to choose a value from the dropDown. 当我尝试从dropDown中选择一个值时,会触发两次具有效果的Ajax。 This is not the desired function. 这不是所需的功能。 As seen below my intention was to bind the button to click and change to dropDown. 如下所示,我的意图是绑定按钮以单击并更改为dropDown。 There there a way I can accomplish this using javascript or jQuery ? 有没有一种方法可以使用javascript或jQuery完成此操作? I'm using a JSP page where I want this functionality. 我正在使用一个JSP页面,我需要此功能。 $('#buttonId #dropDownId').bind('click change' function( event )) { AJAX handler method to perform something. $('#buttonId #dropDownId')。bind('click change'function(event)){AJAX处理程序方法可以执行某些操作。 }); });

Click here for the demo fiddle created with dummy data for illustration purpose. 单击此处 ,使用虚拟数据创建的演示小提琴用于说明目的。

html: HTML:

     <input type="text" id= "accountId" name="account"/>
    <input type="button" id="buttonId"> Submit </input>
                        <br>    
     <select id= "accountType">
    <option value="COM" selected="true">COM </option>            
    <option value="CUR">CUR</option>             
    </select>
   <br>     
    <table id="myTable1">
    <thead>
                        <tr>
                            <th> <strong> something1</strong></th> 
                            <th> <strong>something2</strong></th> 
                         </tr>
                         </thead> 

                <tbody id="TbodyId"></tbody>
    </table>

JS: JS:

            var a = $("#accountType");
    var b = $('#buttonId');
    var $combinedEvent = a.add(b) ;

    console.log("combined event" + $combinedEvent);

    $combinedEvent.bind('click change', function( event ){
    console.log("do something here");
        $('#myTable1').append('<tr><td> one  <td>        
           <td> two </td> </tr>');
       });

I don't think there is any smart shortcut you can use here. 我不认为您可以在此处使用任何智能快捷方式。 The best I can think of is to extract functionality into separate function and pass it as event handler for both onclick and onchange : 我能想到的最好的方法是将功能提取到单独的功能中,并将其作为onclickonchange事件处理程序传递:

function action() {
    $('#myTable1').append('<tr><td> one<td><td> two </td> </tr>');
}

$("#accountType").on('change', action);
$('#buttonId').on('click', action);

Demo: http://jsfiddle.net/Lv08z60m/ 演示: http//jsfiddle.net/Lv08z60m/

Try this : use multiple selector and give comma seperated list of id of button and dropdown. 尝试以下操作:使用多个选择器,并以逗号分隔的按钮ID和下拉列表ID列表。 Also, the .on() method is the preferred method for attaching event handlers to a document over .bind() . 同样, .on()方法是通过.bind()将事件处理程序附加到文档的首选方法。

 $("#accountType,#buttonId").on('click change', function( event ){
        console.log("do something here");
        $('#myTable1').append('<tr><td> one  <td>        
               <td> two </td> </tr>');
    });

Assign some class to the elements to which you want to assign events, and use this class in the selector. 向要为其分配事件的元素分配一些类,然后在选择器中使用该类。 Later there may be added other elements which need the same events to be assigned, and in this case you will need just assign the same class to the element. 稍后可能会添加其他元素,这些元素需要分配相同的事件,在这种情况下,您只需要向该元素分配相同的类即可。

$(".myClassForEvents").on("click change", function(event){

});

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

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