繁体   English   中英

如何取消选中单选按钮

[英]How to Uncheck A radio button

我有两种形式,一种是用户必须选择编辑的单选按钮。

[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);
});

我希望我明确说明我的问题,任何建议都将不胜感激!

你可以像这样访问表格......

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;
   }
}

我不确定你想要什么,但你可能会尝试使用重置输入。

<input type='reset' />

看来这几乎是最简单的DOM任务,并且适用于每个可编写脚本的浏览器,我建议不要使用jQuery方法:

$(".TOR2Hours")[0].checked = false;

对我来说另一件事是你的选择器是否正确。 您的意思是按类选择一组元素还是ID选择器?

你的选择器完全错了。 如果你想从第一个表单中取消选中单选按钮,你应该使用$('input[name="BookItem"]')而不是$('.TOR2Hours')

$("#editBTNcancel").on("click", function(event){ $("#EditFormWrapper").slideUp("fast").empty(); $('input[name="BookItem"]').attr('checked', false); });

至于用于取消选中单选按钮的方法,以下3种方法都应该起作用:

  1. $('input[name="BookItem"]').attr('checked', false);
  2. $('input[name="BookItem"]').removeAttr('checked');
  3. $('input[name="BookItem"]').prop('checked', false);

但是,请查看jQuery的jQuery prop()文档,了解attr()prop()之间的区别。

我用这种方式在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属性!

我刚刚发现了一个很好的解决方案。

假设您有两个需要能够检查/取消选中的无线电,请实施此代码并更改必要的内容:

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; }
    }

:) 别客气

暂无
暂无

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

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