[英]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: 如果您已通过其他方式选择了单选按钮
rb
,则可以执行以下操作:
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: 如果您已经引用了该元素,则可以使用其
checked
属性:
$('input[type=radio]').focus(function(){
// "this" is the element that was clicked
if (this.checked) {
// do something
} else {
// do something else
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.