来自 click() 的 click() 上的 jQuery 奇怪行为
我在 jQuery 1.4 中遇到了点击事件的奇怪行为。这个想法是点击一个下拉列表(change()
),然后使用复选框的 click()
函数反转复选框集合两次。< br> 如果我必须这样做,那是因为复选框上的 click
会更新其他字段,所以我想不要反转复选框 checked
属性,而是对所有复选框调用两次单击复选框。
尽管所有复选框都正确反转了两次,但我在 click
函数上使用的 .length
在操作过程中丢失了正确的计数,并且每次返回的值都比应有的多 1 。
为了清楚起见,代码的想法如下:
//The 'click' function
$('input:checkbox').click(function(){
$(this).parent().find("input:checkbox:checked").length;//<- this one's not returning the right count
//some code updating other fields thanks to the 'length' of the checkboxes
});
//The dropdown function
$('select.dropdown').change(function(){
$(this).parent().find("input:checkbox").click();//Invert the first time
$(this).parent().find("input:checkbox").click();//Invert a second time
});
当我手动检查和取消选中字段时,一切都工作正常,只是当下拉列表调用它时,长度计数错误。
这里有两种情况可以更深入地解释我遇到的问题:
- 如果我从下拉列表中选择某些内容(长度失败),然后取消选中并重新选中复选框,则计数是正确的。
- 如果我在
click()
函数末尾添加有错误的代码,除了在 javascript 调试器控制台上引发错误之外,它可以正常工作。顺便说一句,我无法保留这个解决方案,因为在这种情况下另一个函数失败了。
干杯,
尼古拉斯.
I am facing a strange behaviour in jQuery 1.4 with the click event. The idea is to click on a drop-down list (change()
), which then invert twice a collection of checkboxes by using the click()
function of them.
If I have to do so it is because the click
on checkboxes update other fields, so I thought instead of inverting the checkboxes checked
attribute, let's just call twice the click on all checkboxes.
Although all the checkboxes are correctly inverted twice, the .length
I'm using on the click
function looses the right count during the operation, and everytime returns 1 more than it should.
To make it clear here's the idea of the code:
//The 'click' function
$('input:checkbox').click(function(){
$(this).parent().find("input:checkbox:checked").length;//<- this one's not returning the right count
//some code updating other fields thanks to the 'length' of the checkboxes
});
//The dropdown function
$('select.dropdown').change(function(){
$(this).parent().find("input:checkbox").click();//Invert the first time
$(this).parent().find("input:checkbox").click();//Invert a second time
});
When I manually check and uncheck the fields, everything's working perfectly, it's just when the dropdown call it that the length count is wrong.
Here are 2 scenarios to explain even more in depth the issue I'm experiencing:
- If I choose something from the dropdown (the length fails) and then uncheck and recheck a checkbox, the count is right.
- If I add a buggy code at the end of the
click()
function, it works fine except raising an error on the javascript debugger console. Btw, I cannot keep this solution because another function fails in this case.
Cheers,
Nicolas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想反转复选框的选中
状态,可以执行以下操作:请注意调用.find("input:checkbox")。 attr(...
将反转所有复选框。因此,如果您调用它两次,它们将反转两次,让您看起来什么也没发生。编辑:
如果您只想触发处理程序,则可以使用
.triggerHandler()
它将触发点击
而不触发默认行为元素的http://api.jquery.com/triggerhandler/
If you want to invert thechecked
state of a checkbox, you can do this:Note that calling.find("input:checkbox").attr(...
will invert all the checkboxes. So if you call it twice, they will be inverted twice, giving you the appearance that nothing happened.EDIT:
If the point is that you just want to trigger the handler, you can use
.triggerHandler()
which will trigger theclick
without triggering the default behavior of the element.http://api.jquery.com/triggerhandler/
假设复选框和下拉列表共享相同的表单,为什么不这样做...
那么您不依赖于以嵌套方式处理点击事件(我认为这是问题所在)并且您正在运行 'countCheckedBoxes() ' 显式地运行,而不是依赖于单击复选框的副作用。
Assuming that the checkboxes and the dropdown share the same form, why not do this...
Then you aren't relying on handling click events in a nested manner(which I believe is the problem) and you are running the 'countCheckedBoxes()' function explicitly, instead of relying on a side-effect of the checkboxes being clicked.