jQuery $(“#radioButton”)。更改(...)在取消选择期间不会触发 [英] jQuery $("#radioButton").change(...) not firing during de-selection

查看:76
本文介绍了jQuery $(“#radioButton”)。更改(...)在取消选择期间不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大约一个月前,米特的问题没有得到答复。可悲的是,我现在遇到了同样的情况。



http://api.jquery.com/change/#comment-133939395



以下是我使用jQuery捕获单选按钮中的更改。当选择单选按钮时,我启用编辑框。当取消选择单选按钮时,我希望禁用编辑框。



启用作品。当我在组中选择不同的单选按钮时,更改事件不是被触发。有人知道如何解决这个问题吗?

 < input type =radioid =r1name =someRadioGroup /> 

< script type =text / javascript>
$(#r1)。change(function(){
if($(#r1)。attr(checked)){
$('#r1edit:输入')。removeAttr('disabled');
}
else {
$('#r1edit:input')。attr('disabled',true);
}
});
< / script>


解决方案

看起来像函数仅在您选中单选按钮时调用,而不是在您取消选中时调用。我使用的解决方案是将更改事件绑定到每个单选按钮:

  $(#r1,#r2,#r3 ).change(function(){

或者你可以给所有的单选按钮命名为:

  $(input [name = someRadioGroup]:radio)。change(function(){

以下是一个有效的 jsfiddle示例( )@ b
$ b

Per @ Ray的评论,你应该避免在名称中使用。这些名字可以在jQuery 1.7.2中使用,但不能在其他版本中使用()。


About a month ago Mitt’s question went unanswered. Sadly, I’m running into the same situation now.

http://api.jquery.com/change/#comment-133939395

Here’s the situation: I’m using jQuery to capture the changes in a radio button. When the radio button is selected I enable an edit box. When the radio button is de-selected, I would like the edit box to be disabled.

The enabling works. When I choose a different radio button in the group, the change event is not fired. Does anyone know how to fix this?

<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>

解决方案

Looks like the change() function is only called when you check a radio button, not when you uncheck it. The solution I used is to bind the change event to every radio button:

$("#r1, #r2, #r3").change(function () {

Or you could give all the radio buttons the same name:

$("input[name=someRadioGroup]:radio").change(function () {

Here's a working jsfiddle example (updated from Chris Porter's comment.)

Per @Ray's comment, you should avoid using names with . in them. Those names work in jQuery 1.7.2 but not in other versions (jsfiddle example.).

这篇关于jQuery $(“#radioButton”)。更改(...)在取消选择期间不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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