[英]jQuery if a checkbox is checked, disable other specific checkboxes based on value
Here what I need is, 我需要的是
<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>
Here if I check value 1 or 2 , then value 6 and 7 checkbox must get disabled. 在这里,如果我检查值1 或 2 ,则必须禁用值6 和 7复选框。 And the same way if I check value 6 or 7 , then checkbox value 1 and 2 must get disabled. 同样,如果我检查值6 或 7 ,则必须禁用复选框值1 和 2 。
Suppose if I check value 3 first and then if I checked value 1 or 2, again value 6 and 7 must be get disabled. 假设如果我先检查值3,然后再检查值1或2,则必须再次禁用值6和7。
If I unchecked value 1 and checked value 2, again value 6 and 7 must be get disabled, alternatively 如果我未选中值1和值2,则必须再次禁用值6和7,或者
My code: 我的代码:
$(\'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\');
}
Try this: 尝试这个:
$(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);
}
});
});
Check Demo: https://jsfiddle.net/tusharj/0hoh109k/1/ 检查演示: https : //jsfiddle.net/tusharj/0hoh109k/1/
you have multiple errors in your javascript code. 您的JavaScript代码中有多个错误。 remove the backslash in your jquery selectors as follows: 删除jquery选择器中的反斜杠,如下所示:
$('input[type="checkbox"]')
you also forgot the end brackets in your selectors: 您还忘记了选择器中的尾括号:
$('#employeedetails-relation input[value="6"]')
elseif does not exist in javascript, replace it with: elseif在javascript中不存在,请将其替换为:
else if
here you can find a workin example of your code http://jsfiddle.net/klickagent/up59vsdg/1/ . 在这里您可以找到代码的工作示例http://jsfiddle.net/klickagent/up59vsdg/1/ 。
You can have different click handlers for the set 1,2 or 6,7 then check whether any of those are checked and then set the disabled property 您可以为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>
The Perfect and Tested solution ...Just copy and paste and check 完美且经过测试的解决方案...只需复制,粘贴和检查
<!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.