2015-12-21 195 views
2

我的HTML代碼:jQuery的,如果複選框被選中diasble其他複選框

<input type="checkbox" name="all" value="all">ALL 
<input type="checkbox" name="agent" value="agent" >Agent 
<input type="checkbox" name="company" value="company">Company 
<input type="checkbox" name="branch" value="branch">Branch 

如果我檢查所有其他複選框被禁用,如果我取消,其他複選框又回來了啓用。

我試過這樣做,使用下面的腳本。

$("input[name='all']").change(function() { 
    if(this.checked) { 
     $("input[name='agent']").prop("disabled",true); 
    } 
}); 
+0

檢查答案 –

回答

0

您的代碼只會執行禁用部分。你還是使用checked價值爲禁用屬性基於選中/清除狀態切換狀態:

var subinputs = $("input[name=agent],input[name=company],input[name=branch]"); 
 
$("input[name='all']").change(function() { 
 
    subinputs.prop("disabled",this.checked); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="checkbox" name="all" value="all">ALL 
 
     <input type="checkbox" name="agent" value="agent" >Agent 
 
     <input type="checkbox" name="company" value="company">Company 
 
     <input type="checkbox" name="branch" value="branch">Branch

0

請檢查下面的代碼,還有很多其他的方法,但你的HTML是這樣的方式喲做到這一點

$("input[name='all']").change(function() { 
 
    if(this.checked) { 
 
     $(this).siblings('input:checkbox').prop("disabled",true); 
 
    } 
 
    else { 
 
    $("input[type='checkbox']").removeAttr("disabled"); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="checkbox" name="all" value="all">ALL 
 
     <input type="checkbox" name="agent" value="agent" >Agent 
 
     <input type="checkbox" name="company" value="company">Company 
 
     <input type="checkbox" name="branch" value="branch">Branch

+0

@MilindAnantwar檢查我已經寫下面,以啓用此也嘗試演示,你會看到它工作正常 –

+0

$(「input [name ='all'] 「).removeAttr(」 無效「);這是刪除所有禁用attr行 - –

+0

哦....知道了:) –

0

添加缺少的東西:

$("input[name='all']").change(function() { 
if(this.checked) { 
      $("input[name='all'] input").prop("disabled",true); 
} 
else{ 
     $("input[name='all'] input").prop("disabled",false); 
}}); 
0

綁定的變化處理,那麼就取消所有的複選框,除了一個檢查:下面

$('input').on('change', function() { 
$('input').not(this).prop('checked', false); }); 
1

//I have added a id field in your first check box and add a class rest of checkboxes. 
 
//This code is working you can use this 
 

 
$(function(){ 
 
    $('#all_employee').on('click',function(){ 
 
    if($(this).is(':checked') === true){ 
 
     $('.employee').prop('disabled','disabled'); 
 
    }else{  
 
     $('.employee').prop("disabled", false); 
 
    } 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<input type="checkbox" name="all" value="all" id="all_employee" >ALL 
 
     <input type="checkbox" name="agent" value="agent" class="employee" >Agent 
 
     <input type="checkbox" name="company" value="company" class="employee">Company 
 
     <input type="checkbox" name="branch" value="branch" class="employee">Branch

相關問題