简体   繁体   中英

JQuery: How to determine if a radio button has been selected?

How can I determine if my radio buttons are selected?

For example:

if ( radio_button_selected ) {

// do something

} else {

// do something else

}

You can use this selector to determine if any are checked:

jQuery("input[name='my_button_group']:checked")

So for example:

if (jQuery("input[name='my_button_group']:checked")) {
    ...
}
else {
    ...
}
if ($("input[name='yourRadioName']:radio:checked").length) {
} else {
}

If you have your radio button rb already selected through other means, you can do:

var rb = $('whatever selector');

// other code

if (rb.is(':checked'))
{
  // code
}

If you have a reference to the element already, you can use its checked property:

$('input[type=radio]').focus(function(){
    // "this" is the element that was clicked

    if (this.checked) {
        // do something
    } else {
        // do something else
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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