简体   繁体   English

每个表格仅选中2个复选框

[英]Make only 2 checkboxes checked per form

I have multiple forms in one page with the same input names. 我在一页中有多个具有相同输入名称的表格。 The only difference between forms is the forms that contain them. 表单之间的唯一区别是包含它们的表单。

For the groups of checkboxes on my form, I only want to allow two checkboxes out of three to be checked but I don't want the checkboxes in one form to affect another. 对于我表单上的复选框组,我只希望允许选中三个复选框中的两个,但是我不希望一种表单中的复选框影响另一种复选框。

I have a jsFiddle of what I have now. 我现在有一个jsFiddle。

I have it setup now that only two checkboxes can be checked out of three but that's for the entire document, not form specific. 我现在进行了设置,可以在三个复选框中仅选中两个复选框,但这是针对整个文档的,而不是特定于表格的。

The forms in my page are created with a loop and the id of the form is the only difference between them. 我页面中的表单是用循环创建的,表单的ID是它们之间的唯一区别。

Anyone able to help with this? 任何人都可以帮忙吗?

https://jsfiddle.net/likwidmonster/3zbs61q5/ https://jsfiddle.net/likwidmonster/3zbs61q5/

 $('input[type=checkbox][name=group]').on('change', function(e) { if ($('input[type=checkbox][name=group]:checked').length < 2) { $('input[type=checkbox][name=group]:not(:checked)').prop('disabled', false); } if ($('input[type=checkbox][name=group]:checked').length == 2) { $('input[type=checkbox][name=group]:not(:checked)').prop('disabled', 'disabled'); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <h4> Form 1 </h4> <form id="form_1"> <input type="checkbox" name="group" value="1"> <input type="checkbox" name="group" value="2"> <input type="checkbox" name="group" value="3"> </form> </div> <div> <h4> Form 2 </h4> <form id="form_2"> <input type="checkbox" name="group" value="1"> <input type="checkbox" name="group" value="2"> <input type="checkbox" name="group" value="3"> </form> </div> 

Here's a working solution. 这是一个可行的解决方案。 Hope it helps! 希望能帮助到你!

 $('input[type=checkbox][name=group]').on('change', function(e) { if ($('#form_1 input[type=checkbox][name=group]:checked').length === 2) { $('#form_1 input[type=checkbox][name=group]:not(:checked)').prop('disabled', 'disabled'); }else{ $('#form_1 input[type=checkbox][name=group]:not(:checked)').prop('disabled', false); } if ($('#form_2 input[type=checkbox][name=group]:checked').length === 2) { $('#form_2 input[type=checkbox][name=group]:not(:checked)').prop('disabled', 'disabled'); }else{ $('#form_2 input[type=checkbox][name=group]:not(:checked)').prop('disabled', false); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <div> <h4> Form 1 </h4> <form id="form_1"> <input type="checkbox" name="group" value="1"> <input type="checkbox" name="group" value="2"> <input type="checkbox" name="group" value="3"> </form> </div> <div> <h4> Form 2 </h4> <form id="form_2"> <input type="checkbox" name="group" value="1"> <input type="checkbox" name="group" value="2"> <input type="checkbox" name="group" value="3"> </form> </div> 

Since your forms are dynamic maybe this will be of better use. 由于您的表单是动态的,因此可能会更好用。

I did create a jsfiddle and post it in the comment but that's only good for those two forms. 我确实创建了一个jsfiddle并将其发布在注释中,但这仅对这两种形式有用。 Maybe you want to add more? 也许您想添加更多? JSFiddle Demo JSFiddle演示

this.parentNode.id;

this is used to target the element triggering the function, .parentNode is used to get the form element and .id is used to get the ID of the form so you can target that specific form meaning you only need one if condition to run this for all the forms! this用于定位触发函数的元素, .parentNode用于获取表单元素, .id用于获取表单的ID,因此您可以定位该特定表单,这意味着您仅需要一个if条件来运行该表单所有形式!

 $('form input[type=checkbox][name=group]').on('change', function(e) { var MyForm=this.parentNode.id; if ($('form[id='+MyForm+'] input[type=checkbox][name=group]:checked').length == 2) { $('form[id='+MyForm+'] input[type=checkbox][name=group]:not(:checked)').prop('disabled', 'disabled'); }else{ $('form[id='+MyForm+'] input[type=checkbox][name=group]:not(:checked)').prop('disabled', false); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <h4> Form 1 </h4> <form id="form_1"> <input type="checkbox" name="group" value="1"> <input type="checkbox" name="group" value="2"> <input type="checkbox" name="group" value="3"> </form> </div> <div> <h4> Form 2 </h4> <form id="form_2"> <input type="checkbox" name="group" value="1"> <input type="checkbox" name="group" value="2"> <input type="checkbox" name="group" value="3"> </form> </div> 

If you have any questions please leave a comment below and I will get back to you as soon as possible. 如果您有任何疑问,请在下面发表评论,我们会尽快与您联系。

I hope this help. 希望对您有所帮助。 Happy coding! 编码愉快!

Use a function to initialize your event handler for each group. 使用函数初始化每个组的事件处理程序。 Also use a different group name. 也使用其他组名。

https://jsfiddle.net/3zbs61q5/3/ https://jsfiddle.net/3zbs61q5/3/

 function setupCheckboxGroup(grpName){ $('input[type=checkbox][name='+grpName+']').on('change', function(e) { if ($('input[type=checkbox][name='+grpName+']:checked').length < 2) { $('input[type=checkbox][name='+grpName+']:not(:checked)').prop('disabled', false); } if ($('input[type=checkbox][name='+grpName+']:checked').length == 2) { $('input[type=checkbox][name='+grpName+']:not(:checked)').prop('disabled', 'disabled'); } }); } setupCheckboxGroup('group'); setupCheckboxGroup('group2'); 
 <div> <h4> Form 1 </h4> <form id="form_1"> <input type="checkbox" name="group" value="1"> <input type="checkbox" name="group" value="2"> <input type="checkbox" name="group" value="3"> </form> </div> <div> <h4> Form 2 </h4> <form id="form_2"> <input type="checkbox" name="group2" value="1"> <input type="checkbox" name="group2" value="2"> <input type="checkbox" name="group2" value="3"> </form> </div> 

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

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