为什么代码为child1 radiobuttons提供多个选项?我不想要那个

时间:2011-02-09 13:46:42

标签: jquery

HTML:

<input type="radio" id="chkMain" name="chkMain"/>
<input type="radio" id="chkMain1" name="chkMain" />
<input type="radio" id="chkMain2" name="chkMain" />

<input class="child" type="checkbox" id="chk1" disabled="true" />
<input class="child" type="checkbox" id="chk2" disabled="true" />
<input class="child" type="checkbox" id="chk3" disabled="true" />
<input class="child" type="checkbox" id="chk4" disabled="true" />
<input class="child" type="checkbox" id="chk5" disabled="true" />
<input class="child" type="checkbox" id="chk6" disabled="true" />
<input class="child" type="checkbox" id="chk7" disabled="true" />

<input class="child1" type="radio" id="tone1" disabled="true"/>
<input class="child1" type="radio" id="tone2" disabled="true"/>
<input class="child1" type="radio" id="tone3" disabled="true"/>

脚本:

$( function()
   {
    $( "input:radio[name='chkMain']" ).click( function() {
        if ( $( this ).attr( 'id' ) == 'checkMain' )
        {
            $("input.child").removeAttr( "disabled" );
        }
        else
        {
             $("input.child, input:radio.child1").attr( "disabled");

        }
  });
});

jsFiddle

1 个答案:

答案 0 :(得分:0)

您需要在单选按钮中添加“名称”属性,以指示它们的分组方式如下:

<input class="child1" type="radio" id="tone1" name="group1" disabled="true"/>
<input class="child1" type="radio" id="tone2" name="group1" disabled="true"/>
<input class="child1" type="radio" id="tone3" name="group1" disabled="true"/>

此处的工作示例:http://jsfiddle.net/rniemeyer/dLT5n/

如果语句匹配“chkMain”而不是“checkMain”并且else语句更改为.attr(“disabled”,true),则更改,以便设置该值。