如何验证多个单选按钮

How to Validate Multiple radio buttons

本文关键字:单选按钮 验证 何验证      更新时间:2023-09-26

如何验证多个单选按钮。所有这些单选按钮都是动态生成的。

<input type="radio"  name="answerswer_option1" value="1" id="answers_options1" />
<input type="radio"  name="answerswer_option1" value="2" id="answers_options2" />
<input type="radio"  name="answerswer_option1" value="3" id="answers_options3" />
<input type="radio"  name="answerswer_option1" value="4" id="answers_options4" />
<input type="radio"  name="answerswer_option2" value="5" id="answers_options5" />
<input type="radio"  name="answerswer_option2" value="6" id="answers_options6" />
<input type="radio"  name="answerswer_option2" value="7" id="answers_options7" />
<input type="radio"  name="answerswer_option2" value="8" id="answers_options8" />
<input type="radio"  name="answerswer_option3" value="9" id="answers_options9" />
<input type="radio"  name="answerswer_option3" value="10" id="answers_options10" />
<input type="radio"  name="answerswer_option3" value="11" id="answers_options11" />
<input type="radio"  name="answerswer_option3" value="12" id="answers_options12" />
<input type="radio"  name="answerswer_option4" value="13" id="answers_options13" />
<input type="radio"  name="answerswer_option4" value="14" id="answers_options14" />
<input type="radio"  name="answerswer_option4" value="15" id="answers_options15" />
<input type="radio"  name="answerswer_option4" value="16" id="answers_options16" />

试试这个http://jsfiddle.net/aamir/r9qR2/

由于每个组都有不同的名称属性,因此必须对每组单选按钮进行验证。

if($('input[name="answerswer_option1"]:checked').length === 0) {
     alert('Please select one option');
}

如果您有无限数量的组。试试这个http://jsfiddle.net/aamir/r9qR2/2/

    //Make groups
    var names = []
    $('input:radio').each(function () {
        var rname = $(this).attr('name');
        if ($.inArray(rname, names) === -1) names.push(rname);
    });
    //do validation for each group
    $.each(names, function (i, name) {
        if ($('input[name="' + name + '"]:checked').length === 0) {
            console.log('Please check ' + name);
        }
    });

如果您只想为所有组显示一个错误。试试这个http://jsfiddle.net/aamir/r9qR2/224/

试试这个新小提琴http://jsfiddle.net/Hgpa9/3/

$(document).on("click","#validate", function() { 
var names = [];
    $('input[type="radio"]').each(function() {
        // Creates an array with the names of all the different checkbox group.
        names[$(this).attr('name')] = true;
    });
    // Goes through all the names and make sure there's at least one checked.
    for (name in names) {
        var radio_buttons = $("input[name='" + name + "']");
        if (radio_buttons.filter(':checked').length == 0) {
            alert('none checked in ' + name);
        } 
        else {
            // If you need to use the result you can do so without
            // another (costly) jQuery selector call:
            var val = radio_buttons.val();
        }
    }
});
var names = []
$('input[name^="answerswer_option"]').each(function() {
    var rname = $(this).attr('name');
    if ($.inArray(rname, names) == -1) names.push(rname);
});
$.each(names, function (i, name) {
    if ($('input[name="' + name + '"]:checked').length == 0) {
        console.log('Please check ' + name);
    }
});