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?
We can check the status of a radio button by using the :checked jQuery selector together with the jQuery function is . For example: $('#el').is(':checked') . It is exactly the same method we use to check when a checkbox is checked using jQuery.
First of all, have only one id="test"
Secondly, try this:
if ($('[name="test"]').is(':checked'))
try this
if($('input:radio:checked').length > 0){
// go on with script
}else{
// NOTHING IS CHECKED
}
Something like this:
$("input[name=test]").is(":checked");
Using the jQuery is() function should work.
if($("input:radio[name=test]").is(":checked")){
//Code to append goes here
}
$('#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!');
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With