Javascript 使用动态创建的输入时,模糊事件未按预期工作
我有一个表,其中可以包含一些数字创建的输入,因此用户可以输入一些值并进行一些计算:Javascript 使用动态创建的输入时,模糊事件未按预期工作,javascript,jquery,Javascript,Jquery,我有一个表,其中可以包含一些数字创建的输入,因此用户可以输入一些值并进行一些计算: <table> <thead>...</thead> <tbody> <tr> <td>Info</td> <td><input class="inputsTable"/></td> <td><input c
<table>
<thead>...</thead>
<tbody>
<tr>
<td>Info</td>
<td><input class="inputsTable"/></td>
<td><input class="inputsTable"/></td>
</tbody>
</table>
有时它能工作,但有时它会多次开火。如何解决此问题?如果可行,请尝试此方法。如果要插入或追加动态输入,则应使用
document
重新加载DOM
$(document).on('.inputsTable','input propertychange paste', function () {
if (!isBound) {
isBound = true;
$(this).on('blur', function () {
alert($(this).val());
isBound = false;
});
}
});
或
但是如果你上传完整的代码就可以了。如果可以的话,试试这个。如果要插入或追加动态输入,则应使用
document
重新加载DOM
$(document).on('.inputsTable','input propertychange paste', function () {
if (!isBound) {
isBound = true;
$(this).on('blur', function () {
alert($(this).val());
isBound = false;
});
}
});
或
但是如果您上传完整的代码就可以了。不要在其他事件绑定中创建事件绑定。而是看一下如何使委托事件绑定只在所需条件下工作。我已更改为委托事件绑定,但它仍然多次触发:$('.inputsTable').on('blur',function(event){alert(this.value);return false;//event.stoppropagation或event.preventDefault不起作用});那不是代表<代码>$(parentSelector).on(event,childSelector,eventHandler)是一个委托,该链接详细解释了这一点。
childSelector
是应该使用父选择器和子选择器有条件地尝试的部分,但没有运气:$('td').on('blur','.inputsTable',function(event){event.preventDefault();alert(this.value);});由于on input事件触发了多次,在该事件中,输入中输入的每个字符都会触发该事件,并且由于无条件的错误,因此不会在其他事件绑定中创建事件绑定。而是看一下如何使委托事件绑定只在所需条件下工作。我已更改为委托事件绑定,但它仍然多次触发:$('.inputsTable').on('blur',function(event){alert(this.value);return false;//event.stoppropagation或event.preventDefault不起作用});那不是代表<代码>$(parentSelector).on(event,childSelector,eventHandler)是一个委托,该链接详细解释了这一点。childSelector
是应该使用父选择器和子选择器有条件地尝试的部分,但没有运气:$('td').on('blur','.inputsTable',function(event){event.preventDefault();alert(this.value);});由于on input事件触发了多次,在该事件中,输入中输入的每个字符都会触发该事件,还因为无条件模糊
$(document).on('.inputsTable','input propertychange paste', function () {
if (!isBound) {
isBound = true;
$(this).on('blur', function () {
alert($(this).val());
isBound = false;
});
}
});
$(document).on('.inputsTable','input propertychange paste', function () {
$(this).on('blur', function () {
alert($(this).val());
});
});