使用 JQuery 检查组中是否没有选中单选按钮

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

Using JQuery to check if no radio button in a group has been checked

jquery

提问by Elitmiar

I'm sitting with a problem, I need to check with JQuery if no radio button within a radio button group has been checked, so that I can give the users an javascript error if they forgot to check a option.

我遇到了一个问题,如果没有选中单选按钮组中的单选按钮,我需要使用 JQuery 检查,这样如果用户忘记检查选项,我可以给他们一个 javascript 错误。

I'm using the following code to get the values

我正在使用以下代码来获取值

var radio_button_val = $("input[name='html_elements']:checked").val();

回答by Dominic Rodger

if (!$("input[name='html_elements']:checked").val()) {
   alert('Nothing is checked!');
}
else {
  alert('One of the radio buttons is checked!');
}

回答by Jacobo Hernández

I'm using

我正在使用

$("input:radio[name='html_radio']").is(":checked")

And will return FALSE if all the items in the radiogroup are unchecked and TRUE if an item is checked.

如果无线电组中的所有项目都未选中,则返回 FALSE,如果选中一个项目,则返回 TRUE。

回答by Doug Neiner

You could do something like this:

你可以这样做:

var radio_buttons = $("input[name='html_elements']");
if( radio_buttons.filter(':checked').length == 0){
  // None checked
} else {
  // If you need to use the result you can do so without
  // another (costly) jQuery selector call:
  var val = radio_buttons.val();
}

回答by fanjabi

if ($("input[name='html_elements']:checked").size()==0) {
   alert('Nothing is checked!');
}
else {
  alert('One of the radio buttons is checked!');
}

回答by N Zhang

Use .lengthrefer to http://api.jquery.com/checked-selector/

使用.length参考http://api.jquery.com/checked-selector/

if ($('input[name="html_elements"]:checked').length === 0) alert("Not checked");
else alert("Checked");

回答by David Okwii

var len = $('#your_form_id input:radio:checked').length;
      if (!len) {
        alert("None checked");
      };
      alert("checked: "+ len);

回答by Vitali D.

I think this is a simple example, how to check if a radio in a group of radios was checked.

我认为这是一个简单的例子,如何检查一组收音机中的一个收音机是否被选中。

if($('input[name=html_elements]:checked').length){
    //a radio button was checked
}else{
    //there was no radio button checked
} 

回答by Mathew Magante

I am using this much simple

我正在使用这个非常简单

HTML

HTML

<label class="radio"><input id="job1" type="radio" name="job" value="1" checked>New Job</label>
<label class="radio"><input id="job2" type="radio" name="job" value="2">Updating Job</label>


<button type="button" class="btn btn-primary" onclick="save();">Save</button>

SCRIPT

脚本

 $('#save').on('click', function(e) {
    if (job1.checked)
        {
              alert("New Job"); 
        }
if (job2.checked)
        {
            alert("Updating Job");
        }

}

}