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

时间:2015-05-19 05:45:57

标签: javascript jquery checkbox

我需要的是,

<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\');
        }

4 个答案:

答案 0 :(得分:1)

试试这个:

$(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);
        }

    });
});

检查演示:https://jsfiddle.net/tusharj/0hoh109k/1/

答案 1 :(得分:0)

您的javascript代码中有多个错误。删除jquery选择器中的反斜杠,如下所示:

$('input[type="checkbox"]')

您还忘记了选择器中的结束括号:

$('#employeedetails-relation input[value="6"]')

elseif在javascript中不存在,请将其替换为:

else if

在这里,您可以找到代码http://jsfiddle.net/klickagent/up59vsdg/1/的工作示例。

答案 2 :(得分:0)

您可以为集合1,2或6,7设置不同的点击处理程序,然后检查是否已选中其中任何一个,然后设置已禁用的属性

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>

答案 3 :(得分:0)

完美且经过测试的解决方案......只需复制并粘贴并检查

即可
  <!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>