问题描述
$ 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?
小提琴:
只有在实际修改项目本身时才会发送更改事件。当你点击另一台收音机时,你并没有修改它。一个解决办法就是观察每个输入的变化事件:radio,然后检查相关单选按钮的状态:
$(input:radio)。change(function(){
if($(#rb2)。is(:checked)){
alert('checked');
}
else {
alert('unchecked');
}
});
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没有检测到单选按钮未选中时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!