简体   繁体   English

检查单选按钮是否被选中

[英]check if radio button is checked

I have two radio buttons: 我有两个单选按钮:

<input checked="checked" class="survey_kind_input" type="radio" value="Short">
<input class="survey_kind_input" type="radio" value="Thorough">

I have the following javascript: 我有以下javascript:

if($('.survey_kind_input').checked){
    alert('in survey kind checked');
}

But it is not working. 但这是行不通的。 How do I successfully make the alert work? 如何成功使警报起作用?

You're not checking properly whether it is checked or not, so it won't work. 您没有正确检查是否已检查,因此它将无法正常工作。 Instead, use is() and :checked to check whether it is checked: 而是使用is():checked来检查是否被检查:

 if($('.survey_kind_input').is(':checked')){ alert('in survey kind checked'); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input checked="checked" class="survey_kind_input" type="radio" value="Short"> <input class="survey_kind_input" type="radio" value="Thorough"> 

Either use get() to retrieve the javascript Dom element and use checked or use is(:checked) 使用get()检索javascript Dom元素并使用checked或使用is(:checked)

 if($('.survey_kind_input').get()[0].checked){
        alert('in survey kind checked');
    }

or 要么

if($('.survey_kind_input').is(':checked'){
    alert('in survey kind checked');
}

Try this: 尝试这个:

if($('.survey_kind_input').is(":checked"){
   alert('in survey kind checked');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM