Does binding events to elements in jQuery multiple times have a knock on effect?

前端 未结 2 721
暗喜
暗喜 2021-01-17 14:47

If I have the below code, textboxes of class serial will be bound to the events more than once if the new serial button is pressed multiple times.

Will this hinder

2条回答
  •  无人及你
    2021-01-17 15:14

    Well i think this causes a lot of overhead and some problems because the events are binded more than once. Look at this simple fiddle: http://jsfiddle.net/nicolapeluchetti/syvDu/

    
    
    Serial
    Serial
    Serial
    MonitorSerialTextBoxes(); $('#newSerial').click(function() { MonitorSerialTextBoxes(); }); function MonitorSerialTextBoxes() { $('.serial').each(function() { // Look for changes in the value $(this).bind("click", function(event) { alert("hi"); }); }); }

    When you load the page a single alòert is displayed when you click on a div, but each time you press the button one more alert is displayed because a new event is attached

提交回复
热议问题