如果我理解正确,这是您的情况:
<input type="radio" name="parent_radio1" value="..." />
<input type="radio" name="sub_radio1_1" value="..." />
<input type="radio" name="sub_radio1_2" value="..." />
<input type="radio" name="sub_radio1_3" value="..." />
<input type="radio" name="parent_radio2" value="..." />
<input type="radio" name="sub_radio2_1" value="..." />
<input type="radio" name="sub_radio2_2" value="..." />
<input type="radio" name="sub_radio2_3" value="..." />
如果是这种情况,您可以使用这样的东西(未经测试):
var form = $('#someForm');
form.submit(function(event){
var parent_radios = $('input[type=radio][name^="parent_radio"]');
parent_radios.each(function(){
var parent = $(this);
if (parent.prop('checked') !== true) return; // Parent not selected, no validation needed
var child_selector = parent.attr('name').replace('parent_radio', '');
var childs = $('input[name^="sub_radio' + child_selector + '_"]:checked'); // Select only the selected ones
if (childs.length == 0) {
event.preventDefault(); // No sub radio has been selected for this parent, cancel submit
// Do some other stuff here like show error message
}
});
});