为什么jquery没有检测到单选按钮未选中时? [英] Why isn't jquery detecting when a radio button is unchecked?

查看:100
本文介绍了为什么jquery没有检测到单选按钮未选中时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:


$ b $ j $ $ $(#radioButton).change(...)在取消选择时不会触发



$ b

我有以下HTML / jQuery:

 < input id = rb1type =radioname =rbchecked =true> 
< input id =rb2type =radioname =rb> $($(this).is(:checked)){
($#
$(#rb2)。 alert('checked');
}
else {
alert('unchecked');
}
});

当我的 rb2 单选按钮未被选中时选择rb1,更改事件不会触发。为什么是这样?是否有可能得到这个工作,而不改变我的选择器来匹配两个输入,然后查看ID?



小提琴: http://jsfiddle.net/4uRWR/

解决方案

只有在实际修改项目本身时才会发送更改事件。当你点击另一台收音机时,你并没有修改它。一个解决办法就是观察每个输入的变化事件:radio,然后检查相关单选按钮的状态:

  $(input:radio)。change(function(){
if($(#rb2)。is(:checked)){
alert('checked');
}
else {
alert('unchecked');
}
});

http ://codepen.io/AlienHoboken/pen/akwjB


Possible Duplicate:
JQuery $(#radioButton).change(…) not firing during de-selection

I have the following HTML/jQuery:

<input id="rb1" type="radio" name="rb" checked="true">
<input id="rb2" type="radio" name="rb">


$("#rb2").change(function () {
    if ($(this).is(":checked")) {
         alert('checked');
    }
    else {
        alert('unchecked');
    }
});

When my rb2 radio button is unselected by selecting rb1, the change event does not fire. Why is this? Is it possible to get this working without changing my selector to match both inputs and then looking at the ID?

Fiddle: http://jsfiddle.net/4uRWR/

解决方案

The change event only gets sent when you actually modify the item itself. When you click the other radio, you aren't modifying it. A fix would be to watch the change event on every input:radio, then just check the state of the relevant radio button:

$("input:radio").change(function () {
if ($("#rb2").is(":checked")) {
             alert('checked');
    }
    else {
        alert('unchecked');
    }
});

http://codepen.io/AlienHoboken/pen/akwjB

这篇关于为什么jquery没有检测到单选按钮未选中时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆