检查从每组中选择至少一个单选按钮(使用 jquery)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4028683/
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-08-26 16:32:01  来源:igfitidea点击:

checking at least one radio button is selected from each group (with jquery)

jqueryradio-button

提问by giom

I'm dynamically pulling and displaying radio button quesitons on a form (separated into different sections by jquery tabs).

我在表单上动态拉取和显示单选按钮问题(通过 jquery 选项卡分成不同的部分)。

So there are lots of 2, 3, 4, 5 etc radio buttons together, which have the same 'name' but different ids.

所以有很多 2、3、4、5 等单选按钮在一起,它们具有相同的“名称”但不同的 ID。

When you hit next between the sections, I want to check that at least one radio button has been selected for each group.

当您在各部分之间点击 next 时,我想检查是否已为每一组选择了至少一个单选按钮。

( When the form loads, the radio buttons have none checked by default and it has to be this way. )

(当表单加载时,单选按钮默认没有选中,它必须是这种方式。)

I can loop through the current section's radio buttons but I'm not sure how to easily check that one has been checked for each group?

我可以循环浏览当前部分的单选按钮,但我不确定如何轻松检查是否已为每个组检查过一个?

回答by David says reinstate Monica

In the absence of any relationship between elements, or how to find the group names, I can only give you a pointer, but this should work (the check's triggered by clicking on the element of id="nextButton", but obviously this can be customised to any other appropriate event, such as $('form').submit(), for example):

在元素之间没有任何关系,或者如何找到组名的情况下,我只能给你一个指针,但这应该可以工作(通过单击 的元素触发检查id="nextButton",但显然这可以自定义为任何其他适当的事件,例如$('form').submit(),例如):

$(document).ready(
function(){
  $('#nextButton').click(
    function(){

    var radioName = something; // use this if there's a relationship between the element
                               // triggering this check and the radio buttons

      if ($('input[name='+ radioName +']:checked').length) {
           // at least one of the radio buttons was checked
           return true; // allow whatever action would normally happen to continue
      }
      else {
           // no radio button was checked
           return false; // stop whatever action would normally happen
      }
    }
  );
}
);

回答by eKek0

$("input[@name='the_name']:checked").length;

回答by Aragorn108

if ($("input:checked").length < Num_of_groups)
{
    alert ("Please select atleast one radio button in each group");
    return false;
}

回答by Javid

Try this

尝试这个

if ($('input[name='+radioName+']:checked').length == '0'){
    // do something 
    return false;
}

回答by Wellington Oliveira

Input names must be same name: sendType

输入名称必须相同:sendType

Check this out:

看一下这个:

$('#submitBtn').click(function(e){

   alert ($('input:radio[name="sendType"]:checked').length);

   if ($('input:radio[name="sendType"]:checked').length > 0) {
       //GO AHEAD

   } else {
       alert("SELECT ONE!");
       e.preventDefault();
   }

});

回答by Wellington Oliveira

use this code

使用此代码

remember to put button outside the form

记得把按钮放在表单之外

$("#buttonid").click(function(){
                    var check = true;
                    $("input:radio").each(function(){
                        var name= $(this).attr('name');
                        if($("input:radio[name="+name+"]:checked").length){
                            check = true;
                        }
                        else{
                            check=false;
                        }
                    });

                    if(check){
                        $("#formid").submit();
                    }else{
                        alert('Please select at least one answer in each question.');
                    }
                });