作者:快乐xin_yi | 来源:互联网 | 2023-09-05 16:02
Ifinditweirdthatthe.on()functionisfiringtheeventonlyoncetwiceuponajaxload.我觉得奇怪的是.
I find it weird that the .on()
function is firing the event only once/twice upon ajax load.
我觉得奇怪的是.on()函数在ajax加载时只触发一次/两次事件。
Here's a sample code:
这是一个示例代码:
$(document).on('click', '.cbxCheckAll', function(e) {
alert(this.checked);
$(this).parents('table').find('input.cbxDelete:checkbox').attr('checked', this.checked);
});
Scenario: So basically, I have a dropdown that has a list of users. Upon selecting a user on the dropdown, it instantly loads the selected user's info on a div via ajax. Then on that div, I have multiple checkboxes with a "select all" option.
场景:基本上,我有一个包含用户列表的下拉列表。在下拉列表中选择用户后,它会立即通过ajax在div上加载所选用户的信息。然后在那个div上,我有多个复选框,带有“全选”选项。
Upon clicking the "select all", it did select all checkboxes. Another click, it did unchecked checkboxes. Well, it did work. But when I click again (for the 3rd time), it's not working anymore. It seems to be that it only works on the 2nd click.
单击“全选”后,它会选中所有复选框。再次点击,它取消选中复选框。嗯,确实有效。但是当我再次点击(第3次)时,它不再起作用了。它似乎只适用于第二次点击。
As you may notice, I have an alert()
that displays the value of the "select all" checkbox. It seems to fire everytime I click on it (3rd, 4th, etc.) but the checking/unchecking part is not.
您可能会注意到,我有一个alert(),显示“全选”复选框的值。它似乎每次我点击它(第3,第4等)时都会触发,但是检查/取消检查部分不是。
I just recently updated my Jquery version to the lastest (1.9). I was using the ver (1.7) with the .live()
function and changed it to .on()
since the .live()
was removed on the latest.
我刚刚将我的Jquery版本更新到最新版本(1.9)。我正在使用带有.live()函数的ver(1.7)并将其更改为.on(),因为最近删除了.live()。
Thanks in advance!
提前致谢!
1 个解决方案