jQuery验证-选择至少1个多维数组 [英] jQuery Validation - Select Multidimensional Array at least 1
问题描述
如何使用jQuery Validation验证以下内容.用户必须至少选择以下选项之一.
How can i validate the following using jQuery Validation . The user must at least select 1 of the option below.
<select name="quantity[adult][seat_a]" class="group-required">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<select name="quantity[adult][seat_b]" class="group-required">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<select name="quantity[adult][seat_c]" class="group-required">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
推荐答案
要强制选择这些select
元素中的至少一个,可以使用 additional-methods.js
文件.
To mandate that at least one out of these select
elements are selected, you would use the require_from_group
method which is part of the additional-methods.js
file.
$('#myform').validate({
// other options,
rules: {
"quantity[adult][seat_a]": {
require_from_group: [1, ".group-required"]
},
"quantity[adult][seat_b]": {
require_from_group: [1, ".group-required"]
},
"quantity[adult][seat_c]": {
require_from_group: [1, ".group-required"]
}
},
groups: {
myGroup: "quantity[adult][seat_a] quantity[adult][seat_b] quantity[adult][seat_c]"
}
});
这将同时创建&在所有三个select
元素上的相同错误消息.使用 groups
选项将它们组合为一个和
This will create a simultaneous & identical error message on all three select
elements. Use the groups
option to combine these into one and the errorPlacement
option to place the resulting message into your layout.
重要:您还需要每个select
的默认option
的value
为空.否则,该插件会认为0
是用户的选择,并且不会验证任何内容.
Important: You also need the value
of the default option
of every select
to be empty. Otherwise, the plugin thinks 0
is the user's selection and nothing will be validated.
<select name="quantity[adult][seat_a]" class="group-required">
<option value="">0</option>
....
或...
<select name="quantity[adult][seat_a]" class="group-required">
<option value="">Please select...</option>
<option value="0">0</option>
....
如果您有大量元素,并且不想在.validate()
中单独声明它们,请在jQuery .each()
中使用.rules('add')
方法.
If you have a huge number of elements and do not wish to declare them individually within .validate()
, then use the .rules('add')
method within a jQuery .each()
.
$('#myform').validate({
// other options,
});
$('.group-required').each(function() {
$(this).rules('add', {
require_from_group: [1, '.group-required']
});
});
工作示例: http://jsfiddle.net/n1z0Lufw/
此外,再次使用groups
选项合并错误消息,而不必手动声明每个字段名称...
Additionally, using the groups
option to consolidate error messages, again without having to declare each field name manually...
var names = "";
$('.group-required').each(function() {
names += $(this).attr('name') + " ";
});
names = $.trim(names);
$('#myform').validate({
// other options,
groups: {
myGroup: names
}
});
$('.group-required').each(function () {
$(this).rules('add', {
require_from_group: [1, '.group-required']
});
});
演示2: http://jsfiddle.net/scmuxq53/
DEMO 2: http://jsfiddle.net/scmuxq53/
这篇关于jQuery验证-选择至少1个多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!