【问题标题】:Disable all the checkboxlist items if one of the particular item is selected如果选择了特定项目之一,则禁用所有复选框列表项目
【发布时间】:2013-08-11 12:11:37
【问题描述】:

我有一个复选框列表。其中包含三个项目

  1. 男 2.女性 3.未知

如果有人选择了未知,那么它应该禁用复选框列表上的其他选择。如果选择了未知,则无法选择任何内容。 任何 j 查询、java-script、c# 代码都请帮忙...

【问题讨论】:

    标签: javascript jquery checkbox checkboxlist


    【解决方案1】:

    你可以试试这样的:-

    $('.optionBox input:checkbox').click(function(){
        var $x= $('.optionBox input:checkbox'); 
        if($(this).is(':checked')){ 
           $x.not(this).prop('disabled',true); 
        }
        else
        {
         .....
        }
    })
    

    【讨论】:

    • 这是一个复选框列表,所有三个项目都是此复选框列表中的列表项。所以我不确定我应该在哪里说在上面的代码中检查了该项目......当你的代码运行它时,当任何人被选中时它将禁用,但我需要检查哪个项目被选中。如果选择了该特定项目,我将禁用同一复选框列表中的所有项目。这不是复选框,而是带有项目的单个复选框列表。
    【解决方案2】:

    您可以在 jquery 中使用以下代码来处理此问题。

    $(document).ready(function(){ 
        $("#unknown").change(function(){
            if($(this).is(":checked"))
               $("#male, #female").attr('disabled','disabled');
            else
               $("#male, #female").removeAttr('disabled','disabled');
        });
    });
    

    这是演示http://jsfiddle.net/nKdJg/3/

    【讨论】:

      【解决方案3】:

      在这里,试试这个: http://jsfiddle.net/m8Leu/

      // store the inputs and bind the change event
      var $inputs = $( ".chkGroup input" );
      $inputs.on( "change" , function(e) {
      
        // store the item that changed and the ones that didnt.
        var $changed = $(e.target);
        var $others = $inputs.not( $changed );
      
        // if the item that changed is "unknown"
        if( $changed.hasClass( "unique" ) ) {
      
          // save the state of the "unknown" chk
          // and set the other chks to it's state
          var state = $changed.prop( "checked" )    
          $others.prop( "disabled", state );
      
          // for good measure, we'll uncheck the
          // others because they are disabled
          if( state ) {
            $others.prop( "checked" , false );
          }
      
        }
      
      });
      

      【讨论】:

      • 男性女性Unknown 现在在这里,如果我选择男性或女性或两者都可以,我不需要禁用。但是,如果用户选择了未知,则应禁用其他选项。 @ashish 这在您的代码中意味着您正在传递当前项目,因此无论选择什么,整个控件都将被禁用。这是错误的,我只想在选择未知时才禁用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-13
      • 2016-05-10
      • 2018-06-26
      相关资源
      最近更新 更多