将 :last 和 :checked jQuery 选择器堆叠在一起
我正在尝试为复选框设置自定义验证。我有 7 个复选框,每个复选框都具有相同的名称,我想确定最后一个是否被选中。这是我的代码,我知道它是错误的,任何人都可以阐明如何正确地将 :last 和 :checked 选择器堆叠在一起吗?
$.validator.addMethod('ObserverOtherBox',function(v, e) {
return ($('[[name="4_observers"]:last]:checked').length == 1) && ($('[name="4_observerstxt"]').length == 0) ;
}, 'Please enter the other observers');
I'm trying to setup custom validation for a checkbox. I have 7 checkboxes each with the same name and I want to identify if the last one is checked. This is the code I have and I know its wrong, could anyone shed some light on how to properly stack the :last and :checked selectors together?
$.validator.addMethod('ObserverOtherBox',function(v, e) {
return ($('[[name="4_observers"]:last]:checked').length == 1) && ($('[name="4_observerstxt"]').length == 0) ;
}, 'Please enter the other observers');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
谢谢它成功了。我使用了 .prop('checked'),而不是 is(':checked'),
Thanks it worked. Instead of is(':checked') I used .prop('checked'),
您可以像这样堆叠选择器:
只需将它们附加在一起,没有空格,这适用于所有选择器,无论是:
还是当然这些都是可怕的实际选择器,但您明白了:)
此外,您可以编写特定的代码,例如这个,我认为更清楚一点:
You stack selectors like this:
Just append them together, no spaces, this works for all selectors, whether it's:
Or course those are terrible actual selectors, but you get the point :)
Also as an aside, you can write your particular code like this, a bit clearer I think: