簡體   English   中英

表單中的單選按鈕,如果一個選中,則取消選中其他按鈕?

[英]Radio buttons in the form, if one checked then unchecked the others?

我的表單中有三個不同的單選按鈕組。 如果單擊單選按鈕,則不會將屬性選中設置為true。 我想知道在JQuery / JavaScript中處理此問題的最佳方法是什么? 這是示例:

 $("input[type=radio]").on('click', function() { var secondClick = true; $(this).change(function() { secondClick = false; }); $(this).click(function() { if (secondClick) { $(this).prop("checked", false); } secondClick = true; }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form name="myForm" id="myForm" method="POST" action="#"> <div class="formItem"> <label for="evaluation">Evaluation</label> <input type="radio" name="frm_eval" id="frm_eval1" value="0" />Yes <input type="radio" name="frm_eval" id="frm_eval2" value="1" />No </div> <div class="formItem"> <label for="conditions">Conditions</label> <input type="radio" name="frm_cond" id="frm_cond1" value="0" />Good <input type="radio" name="frmhs_cond" id="frm_cond2" value="1" />Fair <input type="radio" name="frm_cond" id="frm_cond3" value="2" />Poor </div> <div class="formItem"> <label for="responses">Responses</label> <input type="radio" name="frm_res" id="frm_res1" value="0" />Good <input type="radio" name="frm_res" id="frm_res2" value="1" />Fair <input type="radio" name="frm_res" id="frm_res3" value="2" />Poor </div> </form> 

上面的函數沒有更改/設置我單擊的元素上的屬性checked = true。 我希望看到具有選中為true的屬性並將所有其他復選框設置為false的選定元素。

條件中的第二個輸入名為frmhs_cond而不是frm_cond

 <form name="myForm" id="myForm" method="POST" action="#"> <div class="formItem"> <label for="evaluation">Evaluation</label> <input type="radio" name="frm_eval" id="frm_eval1" value="0" />Yes <input type="radio" name="frm_eval" id="frm_eval2" value="1" />No </div> <div class="formItem"> <label for="conditions">Conditions</label> <input type="radio" name="frm_cond" id="frm_cond1" value="0" />Good <input type="radio" name="frm_cond" id="frm_cond2" value="1" />Fair <input type="radio" name="frm_cond" id="frm_cond3" value="2" />Poor </div> <div class="formItem"> <label for="responses">Responses</label> <input type="radio" name="frm_res" id="frm_res1" value="0" />Good <input type="radio" name="frm_res" id="frm_res2" value="1" />Fair <input type="radio" name="frm_res" id="frm_res3" value="2" />Poor </div> </form> 

 var radioInputs = $("#myForm input[type=radio]") radioInputs.on('change',function() { var $this = $(this) if ($this.is(':checked')) { radioInputs.not($this).removeAttr('checked') } }) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM