简体   繁体   English

将两个复选框绑定在一起

[英]Binding two checkboxes together

I have two checkboxes 我有两个复选框

<input class="checkbox1" type="checkbox" name='1' id="example1" value="example1"/> 

and

<input class="checkbox2" type="checkbox" name='2' id="example2" value="example2"/>

Is it possible to connect these two checkboxes, so that whatever happens to the 'checked' attribute of one checkbox also happens to the other? 是否可以连接这两个复选框,以便使一个复选框的“已选中”属性发生的任何事情也发生在另一个复选框上? It needs to work for 它需要为

  1. User changes the checked status 用户更改检查状态

  2. Some javascript function changes the checked status 一些javascript函数更改检查状态

My actual example is very complex and has many many checkboxes. 我的实际示例非常复杂,并且有很多复选框。 So I am looking for a solution where I can just determine a connection between two checkboxes and never have to think about it again. 因此,我正在寻找一种解决方案,只需确定两个复选框之间的连接,而无需再考虑它。 thanks carl 谢谢卡尔

Sure that's possible, but why would you have two checkboxes if they behave the same? 当然可以,但是如果它们的行为相同,为什么还要有两个复选框呢? Maybe one checkbox would be more convenient. 也许一个复选框会更方便。 :) :)

Well, anyway, with a piece of jQuery this should work fine. 好吧,无论如何,使用一块jQuery可以正常工作。 This snippet allows you to give a checkbox a group, so it automatically selects others in the same group. 此代码段使您可以为复选框提供一个组,以便自动选择同一组中的其他组。

Of course you could easily change this into a list of ids for example, so the behaviour doesn't need to be two ways, but I couldn't fully deduce from your question what you need. 当然,您可以轻松地将其更改为一个ID列表,例如,行为不必是两种方式,但是我无法从您的问题中完全推断出您需要什么。 Anyway, adding extra checkboxes just requires them to have the right attribute. 无论如何,添加额外的复选框仅要求它们具有正确的属性。

Because it uses the on function this way, it should even work for checkboxes that are dynamically added. 因为它以这种方式使用on函数,所以它甚至应适用于动态添加的复选框。

 $(document).on('click', 'input[type="checkbox"][data-group]', function(event) { // The checkbox that was clicked var actor = $(this); // The status of that checkbox var checked = actor.prop('checked'); // The group that checkbox is in var group = actor.data('group'); // All checkboxes of that group var checkboxes = $('input[type="checkbox"][data-group="' + group + '"]'); // All checkboxes excluding the one that was clicked var otherCheckboxes = checkboxes.not(actor); // Check those checkboxes otherCheckboxes.prop('checked', checked); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" data-group="group1"> <input type="checkbox" data-group="group2"> <input type="checkbox" data-group="group3"> <input type="checkbox" data-group="group1"> <input type="checkbox" data-group="group2"> <input type="checkbox" data-group="group3"> <input type="checkbox" data-group="group1"> 

You can bind a change event handler on the checkboxes and based on the value of the changed checkbox, set the value of other checkboxes. 您可以在复选框上绑定更改事件处理程序,并根据更改后的复选框的值设置其他复选框的值。

 $(function() { $(".checkbox_class").change(function() { var x = this.checked; $(".checkbox_class").prop("checked", x); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class="checkbox1 checkbox_class" type="checkbox" name='1' id="example1" value="example1" /> <input class="checkbox2 checkbox_class" type="checkbox" name='2' id="example2" value="example2" /> 

It can be binded to change or click event 可以绑定更改或单击事件

 $("#example1").click(function (){ $("#example2").click(); }); $("#example2").click(function (){ $("#example1").click(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class="checkbox1" type="checkbox" name='1' id="example1" value="example1"/> <input class="checkbox2" type="checkbox" name='2' id="example2" value="example2"/> 

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

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