如果我有下面的代码,如果多次按下新的串行按钮,类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只需按照页面上的说明进行操作即可。 –