javascript 如何验证多个单选按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20609863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:53:07  来源:igfitidea点击:

How to Validate Multiple radio buttons

javascriptjqueryradio-button

提问by Talk2Nit

How can I validate multiple radio buttons. All these radio buttons generated dynamically.

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

<input type="radio"  name="answer_option1" value="1" id="ans_options1" />
<input type="radio"  name="answer_option1" value="2" id="ans_options2" />
<input type="radio"  name="answer_option1" value="3" id="ans_options3" />
<input type="radio"  name="answer_option1" value="4" id="ans_options4" />

<input type="radio"  name="answer_option2" value="5" id="ans_options5" />
<input type="radio"  name="answer_option2" value="6" id="ans_options6" />
<input type="radio"  name="answer_option2" value="7" id="ans_options7" />
<input type="radio"  name="answer_option2" value="8" id="ans_options8" />

<input type="radio"  name="answer_option3" value="9" id="ans_options9" />
<input type="radio"  name="answer_option3" value="10" id="ans_options10" />
<input type="radio"  name="answer_option3" value="11" id="ans_options11" />
<input type="radio"  name="answer_option3" value="12" id="ans_options12" />

<input type="radio"  name="answer_option4" value="13" id="ans_options13" />
<input type="radio"  name="answer_option4" value="14" id="ans_options14" />
<input type="radio"  name="answer_option4" value="15" id="ans_options15" />
<input type="radio"  name="answer_option4" value="16" id="ans_options16" />

回答by Aamir Afridi

Try this http://jsfiddle.net/aamir/r9qR2/

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

Since each group has different name attribute so you have to do validation for each set of radio buttons.

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

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

If you have unlimited number of groups. Try this http://jsfiddle.net/aamir/r9qR2/2/

如果您有无限数量的组。试试这个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);
        }
    });

If you want to show just 1 error for all groups. Try this http://jsfiddle.net/aamir/r9qR2/224/

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

回答by patel.milanb

try this new fiddle http://jsfiddle.net/Hgpa9/3/

试试这个新小提琴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();
        }
    }
});

回答by Balachandran

var names = []
$('input[name^="answer_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);
    }
});