【问题标题】:Disable all but one checkbox禁用除一个复选框之外的所有复选框
【发布时间】:2013-01-17 00:03:16
【问题描述】:

我在this on jsfiddle @和我想知道我是如何改变它的,以便且只有在检查其余45时,禁用其余部分,如果如果45未被选中,则启用其余部分。我应该能够在不提交表单的情况下完成所有这些操作。

jsfiddle上的示例javascript:

$(function(){
    $(".rambo").change(function(){
        $(this).siblings().attr("disabled", $(this).is(":checked"));  
    });
});

【问题讨论】:

    标签: javascript jquery html checkbox


    【解决方案1】:

    您可以使用attribute selector

    $(function(){
        $(".rambo[value=1]").change(function(){
            $(this).siblings().prop("disabled", this.checked);  
        });
    });
    

    http://jsfiddle.net/Mnmte/

    【讨论】:

      【解决方案2】:

      只需添加正确的ids 并选择它

      HTML:

      <input type="checkbox" id="cb1" class="rambo" value='1' /> 45 
      <input type="checkbox" id="cb2" class="rambo" value='2' /> 56 
      <input type="checkbox" id="cb3" class="rambo" value='3' /> 56
      

      JS:

      $(function(){
          $("#cb1").change(function(){
              $(this).siblings().attr("disabled", $(this).is(":checked"));  
          });
      });
      

      JSFiddle

      【讨论】:

        【解决方案3】:

        从其他人那里移除 .rambo 类。

        【讨论】:

        • 哦。如果 .rambo 没有样式意义,则更好。
        【解决方案4】:
        $(function(){
            $(".rambo").change(function(){
              if($(this).val() == 1){
                $(this).siblings().attr("disabled", $(this).is(":checked"));  
              }
            });
        });
        

        一种方式

        【讨论】:

          【解决方案5】:

          在所有答案中,您还可以指定只希望第一个checkbox 使用nth-child 选择器执行脚本:

          $(".rambo:nth-child(1)").change(function(){
              $(this).siblings().attr("disabled", $(this).is(":checked"));  
          });
          

          Demo

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-01-22
            • 1970-01-01
            • 2012-02-01
            • 2011-02-25
            • 1970-01-01
            • 1970-01-01
            • 2017-04-21
            • 2016-04-03
            相关资源
            最近更新 更多