如何取消选中单选按钮
我有两种表单,一种带有单选按钮,用户必须选择该按钮才能编辑。
[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>
从表单 (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 //--->
当用户单击表单(B)中的取消按钮(editBTNcancel)时,我想取消选中表单(A)上的单选按钮。
这是我的脚本:
$("#editBTNcancel").live("click", function(event){
event.preventDefault();
$("#EditFormWrapper").slideUp("fast").empty();
//$('.TOR2Hours').removeAttr('checked');
$('.TOR2Hours').attr('checked', false);
});
我希望我清楚地说明我的问题,任何建议将不胜感激!
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)
<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).
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以像这样访问表单...
然后循环遍历
您可以像这样测试检查的表单...
取消选择选中的单选按钮尝试...
最终的代码将如下所示...
you can access form like so ...
then loop through the form
you can test for checked like so ...
to deselect the checked radio button try ...
the final code would look like this ...
我不确定你到底想要什么,但你可以尝试使用重置输入。
I'm not sure exactly what you want but you might try using a reset input.
鉴于这几乎是最简单的 DOM 任务并且适用于每个可编写脚本的浏览器,我建议不要使用 jQuery 方法:
我遇到的另一件事是您的选择器是否正确。您的意思是按类选择一组元素还是应该使用 ID 选择器?
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:
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?
你的选择器根本就是错误的。
如果您想取消选中第一个表单中的单选按钮,您应该使用
$('input[name="BookItem"]')
而不是$('.TOR2Hours')
:$("#editBTNcancel").on("点击", 函数(事件){
$("#EditFormWrapper").slideUp("快速").empty();
$('input[name="BookItem"]').attr('已检查', false);
});
至于使用哪种方法取消选中单选按钮,以下 3 种方法应该都有效:
$('input[name="BookItem"]').attr('checked', false);
$('input[name="BookItem"]').removeAttr('checked');
$('input[name="BookItem"]' ).prop('checked', false);
但是,请查看 jQuery 的文档 jQuery prop() 了解
attr()
和prop()
之间的区别。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')
:$("#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:
$('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()
andprop()
.我刚刚发现了这个问题的一个很好的解决方案。
假设您有两个无线电需要能够被选中/取消选中,请实现此代码并更改必要的内容:
:) 欢迎
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:
:) your welcome
我在ASP.net中用这种方式解决了你的问题,检查一下这个..
在asp.net中,你可以用这种方式添加属性。您还可以手动向radioButton添加属性,并执行函数(clickJs)来更改ckecked属性!!!
I use this way to solve your problem in ASP.net, check this..
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!!!
除非您设置调试断点,否则它始终有效。
当您设置调试断点时它不起作用。
It always works, unless you set debug break points.
It does not work when you set debug break points.