Check if a radio button is checked jquery [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:50:02

问题:

This question already has an answer here:

I have this 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.

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?

回答1:

try this

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


回答2:

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

Secondly, try this:

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


回答3:

Something like this:

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

Using the jQuery is() function should work.



回答4:

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


回答5:

  $('#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!');     }   });


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!