如果我有以下代码,如果多次按下新的串行按钮,类serial的文本框将被多次绑定到事件。
即使多次调用bind方法,这会阻碍性能还是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文档:
如果注册了多个处理程序,它们将始终按照绑定的顺序执行