如果选择单选按钮,jQuery 将验证字段的不同子集

发布于 2025-01-08 00:34:25 字数 116 浏览 4 评论 0原文

我正在使用 jquery 验证插件来验证表单输入。我遇到的问题是我有一个单选按钮组,它根据所选的单选按钮隐藏和显示不同的字段。

如何验证根据选择的单选按钮选项隐藏和显示的字段子集。

谢谢

I am using the jquery validate plugin to validate form input. The problem I have run into is that I have a radio button group that hides and shows different fields based on the radio button selected.

How can I validate the subset of fields that are hidden and shown depending on what radio button option is selected.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

凉城凉梦凉人心 2025-01-15 00:34:25

使用 depends 规则:

$("#myForm").validate({
   rules: {
      contact: {
         required: {
            depends: function(element) {
               return $("#myCheckbox:checked");
            }
         }
      }
   }
})

此示例将 contact 字段设置为仅在选中 #myCheckbox 字段时才需要。

Use the depends rule:

$("#myForm").validate({
   rules: {
      contact: {
         required: {
            depends: function(element) {
               return $("#myCheckbox:checked");
            }
         }
      }
   }
})

This example sets the contact field to only be required is the #myCheckbox field is checked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文