找出是否使用 JQuery 检查了单选按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2272507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:09:23  来源:igfitidea点击:

Find out whether radio button is checked with JQuery?

jqueryhtmljquery-uichecked

提问by Keith Donegan

I can set a radio button to checked fine, but what I want to do is setup a sort of 'listener' that activates when a certain radio button is checked.

我可以将一个单选按钮设置为检查正常,但我想要做的是设置一种“侦听器”,它会在选中某个单选按钮时激活。

Take, for example the following code:

以下面的代码为例:

$("#element").click(function()
{ 
    $('#radio_button').attr("checked", "checked");
});

it adds a checked attribute and all is well, but how would I go about adding an alert. For example, that pops up when the radio button is checked without the help of the click function?

它添加了一个检查属性,一切都很好,但是我将如何添加警报。例如,在没有单击功能的帮助下选中单选按钮时会弹出?

回答by dxh

$('#element').click(function() {
   if($('#radio_button').is(':checked')) { alert("it's checked"); }
});

回答by Parag

If you have a group of radio buttons sharing the same name attribute and upon submit or some event you want to check if one of these radio buttons was checked, you can do this simply by the following code :

如果您有一组共享相同名称属性的单选按钮,并且在提交或某些事件时您想要检查这些单选按钮中的一个是否被选中,您可以通过以下代码简单地完成此操作:

$(document).ready(function(){
  $('#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!');
    }
  });
});

Source

来源

jQuery API Ref

jQuery API 参考

回答by Patrick DaVader

As Parag's solution threw an error for me, here's my solution (combining David Hedlund's and Parag's):

由于 Parag 的解决方案给我带来了错误,这是我的解决方案(结合 David Hedlund 和 Parag 的解决方案):

if (!$("input[name='name']").is(':checked')) {
   alert('Nothing is checked!');
}
else {
   alert('One of the radio buttons is checked!');
}

This worked fine for me!

这对我来说很好用!

回答by Znarkus

You'd have to bind the click event of the checkbox, as the change event doesn't work in IE.

您必须绑定复选框的点击事件,因为更改事件在 IE 中不起作用。

$('#radio_button').click(function(){
    // if ($(this).is(':checked')) alert('is checked'); 
    alert('check-checky-check was changed');
});

Now when you programmatically change the state, you have to trigger this event also:

现在,当您以编程方式更改状态时,您还必须触发此事件:

$('#radio_button').attr("checked", "checked");
$('#radio_button').click();

回答by Nanhe Kumar

//Check through class

//通过类检查

if($("input:radio[class='className']").is(":checked")) {
     //write your code         
}

//Check through name

//通过名字检查

if($("input:radio[name='Name']").is(":checked")) {
         //write your code         
}

//Check through data

//检查数据

if($("input:radio[data-name='value']").is(":checked")) {
         //write your code         
}

回答by Saeb Amini

Another way is to use prop(jQuery >= 1.6):

另一种方法是使用(jQuery >= 1.6)prop

$("input[type=radio]").click(function () {
    if($(this).prop("checked")) { alert("checked!"); }
});

回答by Shah-Kodes

The solution will be simple, As you just need 'listeners' when a certain radio button is checked. Do it :-

解决方案将简单,因为当检查某个单选按钮时,您只需要“侦听器”。这样做:-

if($('#yourRadioButtonId').is(':checked')){ 
// Do your listener's stuff here. 
}

回答by Bill Warren

... Thanks guys... all I needed was the 'value' of the checked radio button where each radio button in the set had a different id...

...谢谢大家...我所需要的只是选中单选按钮的“值”,其中集合中的每个单选按钮都有不同的 ID...

 var user_cat = $("input[name='user_cat']:checked").val();

works for me...

对我有用...

回答by iCezz

If you don't want a click function use Jquery change function

如果您不想要单击功能,请使用 Jquery 更改功能

$('#radio_button :checked').live('change',function(){
alert('Something is checked.');
});

This should be the answer that you are looking for. if you are using Jquery version above 1.9.1 try to use on as live function had been deprecated.

这应该是您正在寻找的答案。如果您使用 1.9.1 以上的 Jquery 版本,请尝试使用 on 因为 live 函数已被弃用。

回答by Thyagarajan C

Working with all types of Radio Buttons and Browsers

使用所有类型的单选按钮和浏览器

if($('#radio_button_id')[0].checked) {
   alert("radiobutton checked")
}
else{
   alert("not checked");
}

Working Jsfiddle Here

在这里工作 Jsfiddle