次のコードがある場合、新しいシリアルボタンが複数回押されると、serialクラスのテキストボックスがイベントに複数回バインドされます。jQueryの要素へのバインディングイベントは何度も有効ですか?
jQueryでは、バインドメソッドが何回も呼び出されても、イベントを1回だけ登録しますか?
$(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
}
});
}
});
UPDATE:私はそれがとてもMonitorSerialTextBoxes機能修正thiingsに以下のコードを追加するだろうと考えていますか? jQueryのドキュメントから
$('.serial').unbind("propertychange keyup input paste");
:
複数のハンドラが登録さがある場合、彼らはいつもあなたがに複数のイベントハンドラをバインドすることができ
私はそれをテストせずに答えは分かりませんが、ビジュアルイベントで自分でテストすることができます。 http://www.sprymedia.co.uk/article/Visual+Eventページの指示に従ってください。 –