Javascript 如何取消选中单选按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2589052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Uncheck A radio button
提问by user281867
I have two forms, one with a radio button that users must select to edit.
我有两种表单,一种带有用户必须选择编辑的单选按钮。
[form name="A"]
<li>[input type="radio" name="BookItem" value="1" /]</li>
<li>[input type="radio" name="BookItem" value="2" /]</li>
<li>[input type="radio" name="BookItem" value="3" /]</li>
[form]<p>
After "BookItem" is selected from form (A) I call the $("#EditFormWrapper").load("callEditData.cfm? ID="+ID);function to load the second form (B)
从表单(A)中选择“BookItem”后,我调用该$("#EditFormWrapper").load("callEditData.cfm? ID="+ID);函数来加载第二个表单(B)
<div id="EditFormWrapper"><div></p>
<!---// begin dynamic form generated by external file callEditData.cfm //--->
[form id="editForm" name="B"]
<ul class="hourswrapper">
<li><input type="checkbox" id="TOR2Hours" class="TOR2Hours" name="TOR2Hours" value="AM2Hrs1" /> 2 Hours AM</li>
<li><input type="checkbox" id="TOR2Hours" class="TOR2Hours" name="TOR2Hours" value="PM2Hrs1" /> 2 Hours PM</li>
<li><input type="checkbox" id="TOR2Hours" class="TOR2Hours" name="TOR2Hours" value="AM2Hrs2" /> 2 Hours AM</li>
<li><input type="checkbox" id="TOR2Hours" class="TOR2Hours" name="TOR2Hours" value="PM2Hrs2" /> 2 Hours PM</li>
</ul>
[input type="image" src="images/submit-btn.gif" id="addBTN" name="addBTN" class="buttons" alt="SubmitRrequest" /]
[input type="image" src="images/cancel-btn.gif" id="editBTNcancel" name="editBTNcancel" class="buttons" alt="Cancel Request" /]
[/form]
<!---// end dynamic form from external file //--->
I want to uncheck the radio button on form (A) when user click on cancel button (editBTNcancel) in form(B).
当用户单击表单 (B) 中的取消按钮 (editBTNcancel) 时,我想取消选中表单 (A) 上的单选按钮。
Here's my script:
这是我的脚本:
$("#editBTNcancel").live("click", function(event){
event.preventDefault();
$("#EditFormWrapper").slideUp("fast").empty();
//$('.TOR2Hours').removeAttr('checked');
$('.TOR2Hours').attr('checked', false);
});
I hope I clearly state my problem, any suggestion would be greatly appreciated!
我希望我清楚地说明我的问题,任何建议将不胜感激!
采纳答案by jeremysawesome
I'm not sure exactly what you want but you might try using a reset input.
我不确定您到底想要什么,但您可以尝试使用重置输入。
<input type='reset' />
回答by brock
you can access form like so ...
你可以像这样访问表单......
var exampleForm = document.forms['form_name'];
then loop through the form
然后遍历表单
for( var i=0; i<exampleForm.length; i++ ){
alert( exampleForm[i].type );
}
you can test for checked like so ...
你可以像这样测试检查......
if( exampleForm[i].checked )
to deselect the checked radio button try ...
取消选中选中的单选按钮尝试...
exampleForm[i].checked=false;
the final code would look like this ...
最终的代码看起来像这样......
var exampleForm = document.forms['form_name'];
for( var i=0; i<exampleForm.length; i++ ){
if( exampleForm[i].type ) == 'radio' && exampleForm[i].checked == true ){
exampleForm[i].checked = false;
}
}
回答by Tim Down
Seeing as this is pretty much the easiest DOM task there is and works in every scriptable browser, I suggest not using the jQuery methods for it:
看到这几乎是最简单的 DOM 任务,并且可以在每个可编写脚本的浏览器中运行,我建议不要使用 jQuery 方法:
$(".TOR2Hours")[0].checked = false;
The other thing that ocurs to me is whether your selector is correct. Did you mean to select a set of elements by class or should it be an ID selector?
对我来说发生的另一件事是您的选择器是否正确。您的意思是按类选择一组元素还是应该是 ID 选择器?
回答by Joel Kornbluh
Your selector is simply wrong.
If you want to uncheck the radio button from first form you should use $('input[name="BookItem"]')and not $('.TOR2Hours'):
您的选择器完全错误。如果您想取消选中第一个表单中的单选按钮,您应该使用$('input[name="BookItem"]')而不是$('.TOR2Hours'):
$("#editBTNcancel").on("click", function(event){
$("#EditFormWrapper").slideUp("fast").empty();
$('input[name="BookItem"]').attr('checked', false);
});
$("#editBTNcancel").on("click", function(event){
$("#EditFormWrapper").slideUp("fast").empty();
$('input[name="BookItem"]').attr('checked', false);
});
As far as which method to use to uncheck radio buttons, The following 3 methods should all work:
至于使用哪种方法取消选中单选按钮,以下3种方法都应该有效:
$('input[name="BookItem"]').attr('checked', false);$('input[name="BookItem"]').removeAttr('checked');$('input[name="BookItem"]').prop('checked', false);
$('input[name="BookItem"]').attr('checked', false);$('input[name="BookItem"]').removeAttr('checked');$('input[name="BookItem"]').prop('checked', false);
However, check out jQuery's docs on jQuery prop()for the difference between attr()and prop().
然而,看看jQuery的文档上jQuery的道具()之间的差额attr()和prop()。
回答by Jason SP Lin
I use this way to solve your problem in ASP.net, check this..
我用这种方式来解决你在 ASP.net 中的问题,检查这个..
radioButton.InputAttributes["show"] = "false";
string clickJs = " if(this.show =='true'){this.show ='false'; this.checked = false;}else{this.show='true'; this.checked = true;};";
radioButton.Attributes["onClick"] = clickJs;
In asp.net, you can use this way to add an attribute. And you can also to add an attribute manually to the radioButton, and do the function(clickJs) to change the ckecked attributes!!!
在asp.net中,可以使用这种方式添加属性。并且你也可以手动给radioButton添加一个属性,并执行函数(clickJs)来改变已勾选的属性!!!
回答by Jacob Gaiski
I just discovered a great solution to this problem.
我刚刚发现了这个问题的一个很好的解决方案。
Assuming you have two radios that need to be able to be checked/unchecked, implement this code and change what's necessary:
假设您有两个无线电需要能够被选中/取消选中,实现此代码并更改必要的内容:
var gift_click = 0;
function HandleGiftClick() {
if (document.getElementById('LeftPanelContent_giftDeed2').checked == true) {
gift_click++;
memorial_click = 0;
}
if (gift_click % 2 == 0) {document.getElementById('LeftPanelContent_giftDeed2').checked = false; }
}
var memorial_click = 0;
function HandleMemorialClick() {
if (document.getElementById('LeftPanelContent_memorialDeed2').checked == true) {
memorial_click++;
gift_click = 0;
}
if (memorial_click % 2 == 0) { document.getElementById('LeftPanelContent_memorialDeed2').checked = false; }
}
:) your welcome
:) 别客气

