jquery Validate:让用户仅选择一个选择框

发布于 2024-12-27 05:34:26 字数 819 浏览 1 评论 0原文

我必须在表单中并排选择框。

<select id="requestID">
...
</select>

OR

<select id="requestor">
...
</select>

我正在使用 jquery 和 jquery 验证插件。 如何确保用户仅从这些选择框中选择一个值?

我已经尝试过depends选项,但这只能让您能够至少进行所需的两个“选择”之一。

我想让用户至少选择且仅选择一个选择值,用户不应避免这两个选择,也不应同时选择这两个选择。

有什么想法吗?

现在这就是我站的地方

rules : {
            requestID: {
                required: {
                    depends: function(element){
                        return !$("#requestor").val().length;
                    }
                }
            },
            requestor: {
                required: {
                    depends: function(element){
                        return !$("#requestID").val().length; 
                    }
                }
            }
}

I have to select boxes side by side in a form.

<select id="requestID">
...
</select>

OR

<select id="requestor">
...
</select>

I am using jquery and the jquery validate plugin.
How can I make sure that the user selects value from only one of these select boxes?

I have tried the depends option, but that only gives you the ability to make at least one of the two 'selects' required.

I want to let the user choose atleast and only one of the select's values, user should not avoid both selects and should not choose both selects.

Any ideas please?

Right now this is where I stand,

rules : {
            requestID: {
                required: {
                    depends: function(element){
                        return !$("#requestor").val().length;
                    }
                }
            },
            requestor: {
                required: {
                    depends: function(element){
                        return !$("#requestID").val().length; 
                    }
                }
            }
}

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

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

发布评论

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

评论(1

扶醉桌前 2025-01-03 05:34:26

您可以编写自定义规则:
jQuery 验证插件 - 如何创建简单的自定义规则?

但我不会在这里使用验证。恕我直言,最好让它像单选按钮一样工作 - 你只是不能检查多个。因此,我会编写一些代码,每当选择一个值时,就会自动取消选择另一个列表框。

You can write a custom rule:
jQuery Validate Plugin - How to create a simple custom rule?

But I wouldn't use a validation here. IMHO it's better to have it work like a radio button - you just can't check more then one. So, I'd write some code that whenever one value is selected, the other list box is automatically being unselected.

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