将 :last 和 :checked jQuery 选择器堆叠在一起

发布于 2024-08-28 05:09:03 字数 365 浏览 4 评论 0原文

我正在尝试为复选框设置自定义验证。我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没有伤那来痛 2024-09-04 05:09:04

谢谢它成功了。我使用了 .prop('checked'),而不是 is(':checked'),

        var lastCheckBox = $('[name="LastCheckboxId"]:last');

        //onload
        $(".someDiv").hide();
        if (lastCheckBox .prop('checked')) {
            $(".someDiv").show();
        }

        //onchange
        lastCheckBox.change(function () {
            if ($(this).prop('checked')) {
                $(".someDiv").show();
            } else {
                $(".someDiv").hide();
            }
        });

Thanks it worked. Instead of is(':checked') I used .prop('checked'),

        var lastCheckBox = $('[name="LastCheckboxId"]:last');

        //onload
        $(".someDiv").hide();
        if (lastCheckBox .prop('checked')) {
            $(".someDiv").show();
        }

        //onchange
        lastCheckBox.change(function () {
            if ($(this).prop('checked')) {
                $(".someDiv").show();
            } else {
                $(".someDiv").hide();
            }
        });
べ繥欢鉨o。 2024-09-04 05:09:03

您可以像这样堆叠选择器:

$('[name="4_observers"]:last:checked')

只需将它们附加在一起,没有空格,这适用于所有选择器,无论是:

$('[name="4_observers"][id=4][rel=bob]')
// or:
$(':input:checkbox:last:checked')

还是当然这些都是可怕的实际选择器,但您明白了:)

此外,您可以编写特定的代码,例如这个,我认为更清楚一点:

$.validator.addMethod('ObserverOtherBox',function(v, e) {
  return $('[name="4_observers"]:last').is(':checked') 
      && $('[name="4_observerstxt"]').length == 0;
}, 'Please enter the other observers');

You stack selectors like this:

$('[name="4_observers"]:last:checked')

Just append them together, no spaces, this works for all selectors, whether it's:

$('[name="4_observers"][id=4][rel=bob]')
// or:
$(':input:checkbox:last:checked')

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:

$.validator.addMethod('ObserverOtherBox',function(v, e) {
  return $('[name="4_observers"]:last').is(':checked') 
      && $('[name="4_observerstxt"]').length == 0;
}, 'Please enter the other observers');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文