user2664298
user2664298

Reputation: 175

Disable all the checkboxlist items if one of the particular item is selected

I have a checkboxlist. which has three items

  1. Male 2.Female 3.Unknown

if someone selected Unknown then it should disable the other selection on the checkboxlist. if Unknown is selected then nothing can be selected. Any j query, java-script, c# code helps please...

Upvotes: 3

Views: 1441

Answers (3)

simey.me
simey.me

Reputation: 2207

Here, try this: 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 );
    }

  }

});

Upvotes: 0

Ashis Kumar
Ashis Kumar

Reputation: 6544

You can use below code in jquery to handle this.

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

Here's the demo http://jsfiddle.net/nKdJg/3/

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172378

You can try something like this:-

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

Upvotes: 1

Related Questions