检查单选按钮是否被选中 jquery

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

Check if a radio button is checked jquery

jquery

提问by Mbrouwer88

I have this HTML:

我有这个 HTML:

<input type="radio" name="test" id="test" value="1"><br>
<input type="radio" name="test" id="test" value="2"><br>
<input type="radio" name="test" id="test" value="3"><br>

<input type="button" onclick="CreateJobDo">

When the page loads, none of them will be checked. I want to use jQuery to check if one of the radio buttons is checked when the button is pressed.

当页面加载时,它们都不会被检查。我想使用 jQuery 来检查按下按钮时是否选中了其中一​​个单选按钮。

I have this:

我有这个:

function CreateJobDo(){
    if ($("input[@name=test]:checked").val() == 'undefined') {

        // go on with script

    } else {

        // NOTHING IS CHECKED
    }
}

But this does not work. How to get it working?

但这不起作用。如何让它工作?

回答by AlienWebguy

First of all, have only one id="test"

首先,只有一个 id="test"

Secondly, try this:

其次,试试这个:

if ($('[name="test"]').is(':checked'))

回答by Mohamed Habib

try this

尝试这个

if($('input:radio:checked').length > 0){
// go on with script
 }else{
    // NOTHING IS CHECKED
 }

回答by Jlange

Something like this:

像这样的东西:

 $("input[name=test]").is(":checked");

Using the jQuery is() function should work.

使用 jQuery is() 函数应该可以工作。

回答by AlphaMale

if($("input:radio[name=test]").is(":checked")){
  //Code to append goes here
}

回答by Ray

  $('#submit_button').click(function() {
    if (!$("input[@name='name']:checked").val()) {
       alert('Nothing is checked!');
        return false;
    }
    else {
      alert('One of the radio buttons is checked!');
    }
  });