ant*_*pes 170 html javascript jquery radio-button
大约一个月前,米特的问题没有得到答复.可悲的是,我现在遇到了同样的情况.
http://api.jquery.com/change/#comment-133939395
情况就是这样:我正在使用jQuery捕获单选按钮中的更改.选中单选按钮后,启用编辑框.取消选中单选按钮后,我希望禁用编辑框.
启用工作.当我在组中选择不同的单选按钮时,不会触发该change
事件.有谁知道如何解决这一问题?
<input type="radio" id="r1" name="someRadioGroup"/>
<script type="text/javascript">
$("#r1").change(function () {
if ($("#r1").attr("checked")) {
$('#r1edit:input').removeAttr('disabled');
}
else {
$('#r1edit:input').attr('disabled', true);
}
});
</script>
Run Code Online (Sandbox Code Playgroud)
And*_*mar 310
看起来该change()
功能仅在您选中单选按钮时调用,而不是在取消选中时调用.我使用的解决方案是将change事件绑定到每个单选按钮:
$("#r1, #r2, #r3").change(function () {
Run Code Online (Sandbox Code Playgroud)
或者您可以为所有单选按钮指定相同的名称:
$("input[name=someRadioGroup]:radio").change(function () {
Run Code Online (Sandbox Code Playgroud)
这是一个有效的jsfiddle示例(更新自Chris Porter的评论.)
Per @ Ray的评论,你应该避免.
在其中使用名称.这些名称在jQuery 1.7.2中有效,但在其他版本中却没有(jsfiddle example.).
Raf*_*fay 35
<input id='r1' type='radio' class='rg' name="asdf"/>
<input id='r2' type='radio' class='rg' name="asdf"/>
<input id='r3' type='radio' class='rg' name="asdf"/>
<input id='r4' type='radio' class='rg' name="asdf"/><br/>
<input type='text' id='r1edit'/>
Run Code Online (Sandbox Code Playgroud)
jquery部分
$(".rg").change(function () {
if ($("#r1").attr("checked")) {
$('#r1edit:input').removeAttr('disabled');
}
else {
$('#r1edit:input').attr('disabled', 'disabled');
}
});
Run Code Online (Sandbox Code Playgroud)
这是DEMO
jte*_*ace 22
您可以通过名称一次绑定到所有单选按钮:
$('input[name=someRadioGroup]:radio').change(...);
Run Code Online (Sandbox Code Playgroud)
这里的工作示例:http: //jsfiddle.net/Ey4fa/