jquery Validate:让用户仅选择一个选择框
我必须在表单中并排选择框。
<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以编写自定义规则:
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.