简体   繁体   English

如何确保只有一个复选框保持选中状态?

[英]How to make sure only a single checkbox remains checked out of many?

I have some HTML with checkboxes (using radio buttons is not convenient due to the library that generates the HTML - to add detail, I'd need to either hack one myself outside of the library or find an unconventional way to make the library work for me). 我有一些带有复选框的HTML格式(由于生成HTML的库,使用单选按钮不方便-要添加详细信息,我需要在库外修改自己的内容,或者寻找一种非常规的方法来使该库适用于我)。

I need to simulate radio button functionality, in particular - when checking a single box out of many, all other boxes should be unchecked. 我需要模拟单选按钮的功能,特别是-在多个中选中一个框时,所有其他框都应取消选中。

I am trying to uncheck all boxes, and then check the one that I clicked. 我试图取消选中所有框,然后选中我单击的框。 My jQuery/JS is failing. 我的jQuery / JS失败了。 Can you help? 你能帮我吗?

 $(function() { //check first box $("input.duty:first").attr("checked", "true"); //clicking unchecked box should check that box //unchecks all others $(".duty").on('click', function(event) { $("input.duty").attr("checked", "false"); $(this).prop("checked", true); }); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="casePointsFieldset"> <fieldset class="casePointFieldset"> <div> <label><span>Duty:</span> <input type="checkbox" name="casePoints[0][isDutyPoint]" class="duty" value="0"></label> </div> </fieldset> <fieldset class="casePointFieldset"> <div> <label><span>Duty:</span> <input type="checkbox" name="casePoints[1][isDutyPoint]" class="duty" value="0"></label> </div> </fieldset> <fieldset class="casePointFieldset"> <div> <label><span>Duty:</span> <input type="checkbox" name="casePoints[2][isDutyPoint]" class="duty" value="0"></label> </div> </fieldset> </div> 

The simplest way to achieve this is to call prop('checked', false) on all the other checkboxes when any one of them is clicked, like this: 实现此目的的最简单方法是prop('checked', false)在单击其他任何一个复选框时prop('checked', false)在所有其他复选框上调用prop('checked', false) ,如下所示:

 $(".duty").first().prop('checked', true).end().on('click', function(event) { $('.duty').not(this).prop('checked', false); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="casePointsFieldset"> <fieldset class="casePointFieldset"> <div> <label> <span>Duty:</span> <input type="checkbox" name="casePoints[0][isDutyPoint]" class="duty" value="0"> </label> </div> </fieldset> <fieldset class="casePointFieldset"> <div> <label> <span>Duty:</span> <input type="checkbox" name="casePoints[1][isDutyPoint]" class="duty" value="0"> </label> </div> </fieldset> <fieldset class="casePointFieldset"> <div> <label> <span>Duty:</span> <input type="checkbox" name="casePoints[2][isDutyPoint]" class="duty" value="0"> </label> </div> </fieldset> </div> 

  • Use .prop in all cases rather than mix .prop / .attr . 在所有情况下.prop使用.prop ,而不要混合使用.prop / .attr
  • use true and false rather than strings. 使用truefalse而不是字符串。

Updated snippet: 更新的代码段:

 $(function() { //check first box $("input.duty:first").prop("checked", true); //clicking unchecked box should check that box //unchecks all others $(".duty").on('click', function(event) { $("input.duty").prop("checked", false); $(this).prop("checked", true); }); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="casePointsFieldset"> <fieldset class="casePointFieldset"> <div> <label><span>Duty:</span> <input type="checkbox" name="casePoints[0][isDutyPoint]" class="duty" value="0"></label> </div> </fieldset> <fieldset class="casePointFieldset"> <div> <label><span>Duty:</span> <input type="checkbox" name="casePoints[1][isDutyPoint]" class="duty" value="0"></label> </div> </fieldset> <fieldset class="casePointFieldset"> <div> <label><span>Duty:</span> <input type="checkbox" name="casePoints[2][isDutyPoint]" class="duty" value="0"></label> </div> </fieldset> </div> 

If you only ever want one checkbox, you should consider radio buttons. 如果您只想要一个复选框,则应考虑使用单选按钮。

https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/radio https://developer.mozilla.org/zh-CN/docs/Mozilla/Tech/XUL/radio

This should fit the use case better. 这应该更适合用例。 Let me know if you need any help with implementation. 让我知道您是否需要任何实施帮助。

I know you say radios are hard because of your use case, but you should seriously consider extending your library with functionality for radio buttons. 我知道您说由于您的用例,收音机很难,但您应该认真考虑使用单选按钮功能扩展库。 It is more correct from HTML standpoint as well as form a UI/UX standpoint. 从HTML的观点以及从UI / UX的观点来看,它是更正确的。

Try to set the checked-property to the boolean false , not to the string "false" . 尝试将检查属性设置为布尔值false ,而不是字符串"false"

Using the string will be interpreted as true . 使用该字符串将被解释为true

$("input.duty").attr("checked", false);

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

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