如果选择了某个特定项,则禁用所有复选框列表项

时间:2013-08-11 12:11:37

标签: javascript jquery checkbox checkboxlist

我有一个复选框列表。这有三个项目

  1. 男 2.Female 3.Unknown
  2. 如果某人选择了“未知”,则应禁用复选框列表中的其他选项。如果选择“未知”,则无法选择任何内容。 任何j查询,java脚本,c#代码都有帮助...

3 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情: -

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

答案 1 :(得分:1)

您可以在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/

答案 2 :(得分:0)

在这里,试试这个: 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 );
    }

  }

});
相关问题