简体   繁体   English

jQuery如果选中了一个复选框,则根据值禁用其他特定的复选框

[英]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.

相关问题 jQuery如果选中一个复选框,请禁用其他复选框 - Jquery If a checkbox is checked disable other checkboxes 如果选中了一个特定选项,则禁用其他复选框 jQuery - Disable other checkboxes if one specific is checked jQuery 如果选中其他复选框,则禁用复选框 - disable checkbox if other checkboxes is checked 如果在ng-repeat中选中了特定复选框,则angularjs禁用其他复选框 - angularjs disable other checkboxes if specific checkbox checked in ng-repeat 当组中的一个复选框被选中时,禁用并取消选中所有其他复选框 - When one checkbox is checked in a group, disable & uncheck all other checkboxes 如果另一个未选中,则禁用复选框 - Disable checkbox if the other isn't checked jQuery 如果选中了“其他”复选框,则取消选中所有复选框并获取价值 - Uncheck all checkboxes if “Other” checkbox is checked and get value (php / jquery)根据另一个选中的复选框值隐藏和显示复选框 - (php/jquery) hide and show checkboxes according to another checked checkbox value 如何从复选框数组中检查至少一个复选框,并且复选框中的文本区域不为空白,且其值为“ other”? - How to check at least one checkbox is checked from the array of checkboxes and the text area is not blank on checkbox with value “other” is checked? Jquery根据复选框选中正确的值 - Jquery getting the correct value based on checkbox checked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM