我有两种形式,一种是用户必须选择编辑的单选按钮。
[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);
});
我希望我明确说明我的问题,任何建议都将不胜感激!
答案 0 :(得分:10)
你可以像这样访问表格......
var exampleForm = document.forms['form_name'];
然后遍历表单
for( var i=0; i<exampleForm.length; i++ ){
alert( exampleForm[i].type );
}
你可以像这样测试被检查......
if( exampleForm[i].checked )
取消选中已选中的单选按钮尝试...
exampleForm[i].checked=false;
最终代码看起来像这样......
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;
}
}
答案 1 :(得分:7)
我不确定你想要什么,但你可能会尝试使用重置输入。
<input type='reset' />
答案 2 :(得分:3)
看到这几乎是最简单的DOM任务,并且在每个可编写脚本的浏览器中都有效,我建议不要使用jQuery方法:
$(".TOR2Hours")[0].checked = false;
对我而言,另一件事是你的选择器是否正确。您的意思是按类选择一组元素还是ID选择器?
答案 3 :(得分:1)
你的选择器完全错了。
如果您要取消选中第一个表单中的单选按钮,则应使用$('input[name="BookItem"]')
而不是$('.TOR2Hours')
:
$("#editBTNcancel").on("click", function(event){
$("#EditFormWrapper").slideUp("fast").empty();
$('input[name="BookItem"]').attr('checked', false);
});
对于取消选中单选按钮的方法,以下3种方法都可以正常工作:
$('input[name="BookItem"]').attr('checked', false);
$('input[name="BookItem"]').removeAttr('checked');
$('input[name="BookItem"]').prop('checked', false);
但是,请查看jQuery关于jQuery prop()的文档,了解attr()
和prop()
之间的区别。
答案 4 :(得分:0)
我刚刚发现了解决这个问题的好方法。
假设您有两个无线电需要能够被选中/取消选中,请实施此代码并更改必要的内容:
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; }
}
:)欢迎你
答案 5 :(得分:0)
我用这种方式在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;
在asp.net中,您可以使用这种方式添加属性。您还可以手动将属性添加到radioButton,并执行该功能(clickJs)以更改ckecked属性!!!