如果我有下面的代碼,如果多次按下新的串行按鈕,類serial的文本框將被綁定到事件多次。多次將事件綁定到jQuery中的元素有一個敲門效應?
即使綁定方法被調用很多次,這是否會妨礙性能或者jQuery是否只註冊一次事件?
$(document).ready(function() {
MonitorSerialTextBoxes();
$('#newSerial').click(function() {
$.tmpl("productTemplate", mymodel).insertAfter($(".entry").last());
MonitorSerialTextBoxes();
});
function MonitorSerialTextBoxes() {
$('.serial').each(function() {
// Save current value of element
$(this).data('oldVal', $(this).val());
// Look for changes in the value
$(this).bind("propertychange keyup input paste", function (event) {
// If value has changed...
if ($(this).data('oldVal') != $(this).val() && $(this).val().length == 10) {
// Updated stored value
$(this).data('oldVal', $(this).val());
// Do action
}
});
}
});
更新:我相信它會做會增加下面的代碼到MonitorSerialTextBoxes功能修復thiings?
$('.serial').unbind("propertychange keyup input paste");
從jQuery的文檔:
如果有註冊多個處理程序,他們將永遠在他們被束縛
我不確定沒有測試它的答案,但你可以用Visual Event自己測試它。 http://www.sprymedia.co.uk/article/Visual+Event只需按照頁面上的說明進行操作即可。 –