简体   繁体   English

JavaScript中的动态表和事件委托

[英]Dynamic Table and event delegation in Javascript

I have a button that adds a row in mytable 我有一个在mytable中添加一行的按钮

   function addRow() {

    $('#mytable').append('<tr>' +
    '<td ><div style="width:187px">' +
    ' <input type="text" value="" style="width: 150px" name="item.Account_Code"  >' +
    '<button type=\'button\' style="position: relative; left:3px" id="AccCode">...</button>' +
    '</div></td>' +
    '<td><input type="text" style="width: 200px" name="item.Description"></td>' +
    '<td><input type="text" value="0.00" style="width: 100px" name="item.Debit_Amt" onkeydown="return ValidateNumber(event);"></td>' +
    '</tr>')};

When i try to catch a td with event handler the result of the events are triggering multiple times. 当我尝试使用事件处理程序捕获td时,事件的结果将触发多次。 For example, in this case the alert message is popping up mutiple time. 例如,在这种情况下,警报消息会弹出多个时间。

    $('#mytable').delegate("td", "blur", function () {

    var self = $(this);
    var tr = self.closest('tr');
    index = tr.index();

     $('#mytable').find('tr:eq(' + index + ')').find('td:eq(1)').find('input[type=text]').blur(function () {
     alert("abcd");
     )}
     )}

First time i blur out of the td, shows 1 alert message. 第一次我模糊掉td时,显示1条警告消息。 But the next time i blur out, comes 2 or more alert messages instead of 1. The more i blur out of that td, a lot more alert messages pop up instead of 1.What can be the reason and how to fix it? 但是,下次我脱口而出时,出现2条或更多的警报消息,而不是1条。我从该td中脱口而出的次数越多,弹出的警报消息就越多,而不是1条。这是什么原因以及如何解决它?

Because each time the blur happens, you are adding a new blur handler to the input element. 因为每次模糊发生,您都要向输入元素添加一个新的blur处理程序。

Instead what you need is a single delegated event handler like below, also since you have name for your input elements, target the input element with the name attribute value. 取而代之的是,您需要的是如下所示的单个委派事件处理程序,同样,由于您拥有输入元素的name ,因此请使用name属性值来定位输入元素。

 function addRow() { $('#mytable').append('<tr>' + '<td ><div style="width:187px">' + ' <input type="text" value="" style="width: 150px" name="item.Account_Code" >' + '<button type=\\'button\\' style="position: relative; left:3px" id="AccCode">...</button>' + '</div></td>' + '<td><input type="text" style="width: 200px" name="item.Description"></td>' + '<td><input type="text" value="0.00" style="width: 100px" name="item.Debit_Amt" onkeydown="return ValidateNumber(event);"></td>' + '</tr>') }; $('#mytable').on('blur', 'input[name="item.Description"]', function () { //or $('#mytable').delegate('input[name="item.Description"]', "blur", function () { alert("abcd"); }); $('button').click(addRow); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <button id="">Add</button> <table id="mytable"></table> 

You are attaching blur event to the text box inside of a blur event handler. 您要将模糊事件附加到模糊事件处理程序内部的文本框中。 This basically causes a new event handler to be attached every time you blur out resulting in the alert being fired multiple times. 基本上,这会使您每次脱口而出一个新的事件处理程序,从而导致多次触发警报。 You could either stop bubbling the blur event from the text box or not bind the blur of text box inside td's blur but do it in some other place. 您可以停止从文本框中冒出模糊事件,也可以不将文本框的模糊绑定到td的模糊内,而在其他地方进行。

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

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