我可以设置检查罚款单选按钮,但我想要做的是建立一种在某个单选按钮被选中激活“听众”的。

<强>径,例如下面的代码:

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

它增加了一个检查属性,一切都很好,但我将如何去 添加警报。例如,当无线电按钮被选中弹出 无需点击功能的帮助?

有帮助吗?

解决方案

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

其他提示

如果你有一个单选按钮组共享相同的名称属性和在提交或你需要一些事件来检查,如果这些一个单选按钮被选中,你可以做到这一点简单地通过下面的代码:

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

jQuery的API编号

由于Parag的解决方案抛出错误对我来说,这里是我的解决方案(结合大卫Hedlund的年代和Parag的):

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

这工作得很好,我!

您不得不复选框的click事件绑定,作为变更事件不会在IE浏览器。

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

现在,当您以编程方式更改的状态,你也必须触发该事件:

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

//检查通过类

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

//检查通过名称

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

//检查通过数据

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

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

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

该解决方案将是简单的,因为只需要“听众”,当特定无线按钮被选中。做到这一点: -

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

如果您不想点击功能 使用jQuery改变功能

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

这应该是你正在寻找的答案。 如果你正在使用1.9.1以上版本的Jquery 尝试为肝功能已被弃用即可使用。

...感谢球员...所有我需要是所检查的单选按钮,其中集合中的每个单选按钮具有不同的ID的“价值” ...

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

对我的作品......

<强>与所有类型的单选按钮和浏览器的工作

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

工作的jsfiddle这里

生成的动态单选按钮检查无线电GET值

$("input:radio[name=radiobuttonname:checked").val();
  

在变化动态单选按钮

$('input[name^="radioname"]').change(function () {if (this.value == 2) { }else{}});

$(“单选按钮类名。‘)是(’检查”),我没有工作,但接下来的代码运行良好。

    if(typeof $('.radio-button-class-name:checked').val() !== 'undefined'){
     // radio button is checked
    }

尝试此

    if($('input[name="radiobutton"]:checked').length == 0) {
        alert("Radio buttons are not checked");
    }

尝试这种情况:

alert($('#radiobutton')[0].checked)

jQuery是仍然很受欢迎,但如果你想有没有依赖性,见下文。 短&清除功能,以找出是否单选按钮上ES-2015检查:

function getValueFromRadioButton( name ){
  return [...document.getElementsByName(name)]
         .reduce( (rez, btn) => (btn.checked ? btn.value : rez), null)
}

console.log( getValueFromRadioButton('payment') );
<div>  
  <input type="radio" name="payment" value="offline">
  <input type="radio" name="payment" value="online">
  <input type="radio" name="payment" value="part" checked>
  <input type="radio" name="payment" value="free">
</div>

$("#radio_1").prop("checked", true);

有关之前1.6 jQuery的版本中,使用:

$("#radio_1").attr('checked', 'checked');
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top