[英]jQuery if a checkbox is checked, disable other specific checkboxes based on value
我需要的是
<div id="employeedetails-relation" class="check"><div class="checkbox"> <label><input type="checkbox" name="Employeedetails[relation][]" value="1"> Father</label></div>
<div class="checkbox"><label><input type="checkbox" name="Employeedetails[relation][]" value="2"> Mother</label></div>
<div class="checkbox"><label><input type="checkbox" name="Employeedetails[relation][]" value="3"> Spouse</label></div>
<div class="checkbox"><label><input type="checkbox" name="Employeedetails[relation][]" value="4"> Child1</label></div>
<div class="checkbox"><label><input type="checkbox" name="Employeedetails[relation][]" value="5"> Child2</label></div>
<div class="checkbox"><label><input type="checkbox" name="Employeedetails[relation][]" value="6"> Father-in-law</label></div>
<div class="checkbox"><label><input type="checkbox" name="Employeedetails[relation][]" value="7"> Mother-in-law</label></div></div>
在這里,如果我檢查值1 或 2 ,則必須禁用值6 和 7復選框。 同樣,如果我檢查值6 或 7 ,則必須禁用復選框值1 和 2 。
假設如果我先檢查值3,然后再檢查值1或2,則必須再次禁用值6和7。
如果我未選中值1和值2,則必須再次禁用值6和7,或者
我的代碼:
$(\'input[type="checkbox"]\').click(function(){
if (this.checked && this.value === "1") {
$(\'#employeedetails-relation input[value="6"\').prop(\'disabled\', \'true\');
$(\'#employeedetails-relation input[value="7"\').prop(\'disabled\', \'true\');
}
elseif (!this.checked && this.value === "1")
{
$(\'#employeedetails-relation input[value="6"\').prop(\'disabled\', \'false\');
$(\'#employeedetails-relation input[value="7"\').prop(\'disabled\', \'false\');
}
嘗試這個:
$(document).ready(function() {
$('#employeedetails-relation').on('change', ':checkbox', function() {
var value = parseInt($(this).val());
var checkedEl = [];
$(':checkbox:checked').each(function() {
checkedEl.push(parseInt($(this).val()));
});
$('#employeedetails-relation :checkbox').prop('disabled', false);
if ($.inArray(1, checkedEl) > -1 || $.inArray(2, checkedEl) > -1) {
$(':checkbox[value=6]').prop('disabled', true);
$(':checkbox[value=7]').prop('disabled', true);
} else if ($.inArray(6, checkedEl) > -1 || $.inArray(7, checkedEl) > -1) {
$(':checkbox[value=1]').prop('disabled', true);
$(':checkbox[value=2]').prop('disabled', true);
}
});
});
您的JavaScript代碼中有多個錯誤。 刪除jquery選擇器中的反斜杠,如下所示:
$('input[type="checkbox"]')
您還忘記了選擇器中的尾括號:
$('#employeedetails-relation input[value="6"]')
elseif在javascript中不存在,請將其替換為:
else if
在這里您可以找到代碼的工作示例http://jsfiddle.net/klickagent/up59vsdg/1/ 。
您可以為1,2或6,7設置不同的單擊處理程序,然后檢查是否選中了其中的任何一個,然后設置Disabled屬性
var $checks12 = $('#employeedetails-relation').find('input[value="1"], input[value="2"]').click(function() { $checks67.prop('disabled', $checks12.is(':checked')); }); var $checks67 = $('#employeedetails-relation').find('input[value="6"], input[value="7"]').click(function() { $checks12.prop('disabled', $checks67.is(':checked')); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="employeedetails-relation" class="check"> <div class="checkbox"><label><input type="checkbox" name="Employeedetails[relation][]" value="1"/> Father</label></div> <div class="checkbox"><label><input type="checkbox" name="Employeedetails[relation][]" value="2"/> Mother</label></div> <div class="checkbox"><label><input type="checkbox" name="Employeedetails[relation][]" value="3"/> Spouse</label></div> <div class="checkbox"><label><input type="checkbox" name="Employeedetails[relation][]" value="4"/> Child1</label></div> <div class="checkbox"><label><input type="checkbox" name="Employeedetails[relation][]" value="5"/> Child2</label></div> <div class="checkbox"><label><input type="checkbox" name="Employeedetails[relation][]" value="6"/> Father-in-law</label></div> <div class="checkbox"><label><input type="checkbox" name="Employeedetails[relation][]" value="7"/> Mother-in-law</label></div> </div>
完美且經過測試的解決方案...只需復制,粘貼和檢查
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
$(document).ready(function(){
$('input[type="checkbox"]').click(function(){
if($(this).val()==1 || $(this).val()==2 ){
$('#checkbox6').attr('disabled','true');
$('#checkbox7').attr('disabled','true');
}
})
// var check= document.getElementsByName('Employeedetails[relation][]');
// alert(check.val);
});
</script>
<meta charset="windows-1252">
<title></title>
</head>
<body>
<?php
// put your code here
?>
<div id="employeedetails-relation" class="check"><div class="checkbox"> <label><input id="checkbox1" type="checkbox" name="Employeedetails[relation][]" value="1"> Father</label></div>
<div class="checkbox"><label><input id="checkbox2" type="checkbox" name="Employeedetails[relation][]" value="2"> Mother</label></div>
<div class="checkbox"><label><input id="checkbox3" type="checkbox" name="Employeedetails[relation][]" value="3"> Spouse</label></div>
<div class="checkbox"><label><input id="checkbox4" type="checkbox" name="Employeedetails[relation][]" value="4"> Child1</label></div>
<div class="checkbox"><label><input id="checkbox5" type="checkbox" name="Employeedetails[relation][]" value="5"> Child2</label></div>
<div class="checkbox"><label><input id="checkbox6" type="checkbox" name="Employeedetails[relation][]" value="6"> Father-in-law</label></div>
<div class="checkbox"><label><input id="checkbox7" type="checkbox" name="Employeedetails[relation][]" value="7"> Mother-in-law</label></div> </div>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.