简体   繁体   English

jQuery,仅在未选中其他两个复选框的情况下才取消选中复选框

[英]Jquery, Uncheck checkbox only if two other checkboxes are unchecked

In my website, the user can create 'contact categories','item categories' and can decide which contact category is associated with which item categories. 在我的网站上,用户可以创建“联系人类别”,“项目类别”,并可以确定哪个联系人类别与哪些项目类别相关联。 Multiple item categories can be associated with a contact category and vise versa. 多个项目类别可以与联系人类别关联,反之亦然。

https://jsfiddle.net/vuuxn5gf/ https://jsfiddle.net/vuuxn5gf/

Here is the HTML code. 这是HTML代码。

<h1>
  New Contact
</h1>

<input type="checkbox" value="1" id="user_name">Contact category 1<br>
<input type="checkbox" value="2" id="user_name">Contact category 2<br>
<input type="checkbox" value="3" id="user_name">Contact category 3<br><br>

<input type="checkbox" value="a" id="user_name">Item category a<br>
<input type="checkbox" value="b" id="user_name">Item category b<br>
<input type="checkbox" value="c" id="user_name">Item category c

and the JQuery 和JQuery

var chk1 = $("input[type='checkbox'][value='1']");
var chk2 = $("input[type='checkbox'][value='2']");
var chk3 = $("input[type='checkbox'][value='3']");

var chka = $("input[type='checkbox'][value='a']");
var chkb = $("input[type='checkbox'][value='b']");
var chkc = $("input[type='checkbox'][value='c']");

chk1.on('change', function() {
  chka.prop('checked', this.checked);
  chkb.prop('checked', this.checked);
});

chk2.on('change', function() {
  chkb.prop('checked', this.checked);
});

chk3.on('change', function() {
  chka.prop('checked', this.checked);
  chkc.prop('checked', this.checked);
});

In this example contact category 1 is associated to item category a & b, contact category 2 is associated to item category b and contact category 3 with item category a & c. 在此示例中,联系人类别1与项目类别a和b相关联,联系人类别2与项目类别b相关联,联系人类别3与项目类别a&c相关联。

SITUATION: A user adds a new contact. 情况:用户添加了一个新联系人。 The user places the new contact to contact category 1 & 2. That way, Item category a & b get's activated through the JQuery code. 用户将新联系人放置在联系人类别1和2中。这样,可以通过JQuery代码激活项目类别a和b。

PROBLEM: If the user unchecks contact category 1, both item category a & b get's deactivated. 问题:如果用户取消选择联系人类别1,则项目类别a和b均被停用。 While item category b should still be checked, because contact category 2 is still checked. 尽管仍应检查项目类别b,因为仍要检查联系类别2。

SIDENOTE: In practice, this won't be just 3 categories each. 旁白:实际上,这不仅是3个类别。 But a larger scale, so I hope this can be solved with something like. 但是规模更大,所以我希望可以用类似的方法解决。

chk1.on('checked', function() {
  chka++;
  chkb++;
if (chka > 0) chka.prop('change', this.checked);
});

chk1.on('unchecked', function() {
  chka--;
  chkb--;
if (chka > 0) chka.prop('change', this.checked);
});

One issue you have with your current code is that the only reference to the relationship between the contact categories and the item categories is within each function that handles the change events. 当前代码存在的一个问题是,在处理变更事件的每个函数中,仅是对联系人类别和项目类别之间关系的唯一引用。

With this method there is no way for contact category 1 to check that there is a link between contact category 2 and item category b. 使用此方法,联系人类别1无法检查联系人类别2和项目类别b之间是否存在链接。

You'll want to create some variable to hold these relationships, and then within each change event check for other relationships that might affect whether the item category should be changed or not. 您将需要创建一些变量来保存这些关系,然后在每个更改事件中检查可能影响项目类别是否应更改的其他关系。

I modified your fiddle here: https://jsfiddle.net/vuuxn5gf/3/ 我在这里修改了您的小提琴: https : //jsfiddle.net/vuuxn5gf/3/

In which I made a matrix to hold the relationship between the different categories. 在其中我建立了一个矩阵来保存不同类别之间的关系。 This is just an example, you could have the matrix going the other way instead, but this is how I defined it: 这只是一个示例,您可以使矩阵相反,但这是我定义的方式:

var icats = [];
icats['a'] = ['1','3'];
icats['b'] = ['1','2'];
icats['c'] = ['3'];

and then I made a function to handle the contact category change events and for each one go through the item category checkboxes and for each of those perform the checks on the matrix to handle if other contact categories are checked. 然后,我制作了一个函数来处理联系人类别更改事件,并针对每个选项进行了项目类别复选框,并对每个复选框都在矩阵上进行了检查,以处理是否检查了其他联系人类别。

$("#contact-cats input:checkbox").on('change', function(){
  var idx = $(this).val();
  $("#item-cats input:checkbox").each(function(){
    var itm = $(this).val();
    var arr = icats[itm];
    var skipChange = false;
    var makeChange = false;
    for(var i=0;i<arr.length;i++){
      if(arr[i] != idx){
         skipChange = $("#contact-cats input:checkbox[value="+arr[i]+"]").prop("checked");
      }else{
         makeChange = true;
      }
    }
    if(makeChange && !skipChange){
      $(this).prop("checked", !$(this).prop("checked"));
    }
  });
});

I admit this isn't the cleanest, but I hope it gets across the idea you'll need for your own solution. 我承认这不是最干净的方法,但是我希望它能解决您自己的解决方案所需的想法。

I updated your ids to be unique and added a 'redoit' function to reapply the checked status after you uncheck, for example uncheck cb1 while cb2 is checked. 我将您的ID更新为唯一,并添加了“重做”功能以在取消选中后重新应用已选中的状态,例如,在选中cb2时取消选中cb1。 I also got rid of the vars for brevity. 为了简洁起见,我也放弃了vars。 I hope this helps! 我希望这有帮助!

HTML: HTML:

<h1>
New Contact
</h1>

<input type="checkbox" value="1" id="cb1">Contact category 1
<br>
<input type="checkbox" value="2" id="cb2">Contact category 2
<br>
<input type="checkbox" value="3" id="cb3">Contact category 3
<br>
<br>

<input type="checkbox" value="a" id="cba">Item category a
<br>
<input type="checkbox" value="b" id="cbb">Item category b
<br>
<input type="checkbox" value="c" id="cbc">Item category c

jQuery: jQuery的:

$("#cb1").on('change', function() {
  $("#cba").prop('checked', this.checked);
  $("#cbb").prop('checked', this.checked);
  redoit();
});

$("#cb2").on('change', function() {
  $("#cbb").prop('checked', this.checked);
  redoit();
});

$("#cb3").on('change', function() {
  $("#cba").prop('checked', this.checked);
  $("#cbc").prop('checked', this.checked);
  redoit();
});

function redoit() {
  if ($("#cb1").is(':checked')) {
    $("#cba").prop('checked', true);
    $("#cbb").prop('checked', true);
  }
  if ($("#cb2").is(':checked')) {
    $("#cbb").prop('checked', true);
  }
  if ($("#cb3").is(':checked')) {
      $("#cba").prop('checked', true);
    $("#cbc").prop('checked', true);
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM