Javascript 如何清除Javascript中的单选按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2554116/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:52:45  来源:igfitidea点击:

How to clear radio button in Javascript?

javascripthtml

提问by i2ijeya

I have a radio button named "Choose" with the options yes and no. If I select any one of the options and click the button labeled "clear", I need to clear the selected option, using javascript. How can I accomplish that?

我有一个名为“选择”的单选按钮,其中包含选项是和否。如果我选择任何一个选项并单击标记为“清除”的按钮,我需要使用 javascript 清除所选选项。我怎样才能做到这一点?

回答by NVRAM

You don't need to have unique idfor the elements, you can access them by their nameattribute:

您不需要id元素的唯一性,您可以通过它们的name属性访问它们:

If you're using name="Choose", then:

如果您正在使用name="Choose",则:

  • With jQuery it is as simple as:

    $('input[name=Choose]').attr('checked',false);
    
  • or in pure JavaScript:

       var ele = document.getElementsByName("Choose");
       for(var i=0;i<ele.length;i++)
          ele[i].checked = false;
    

    Demo for JavaScript

  • 使用 jQuery,它很简单:

    $('input[name=Choose]').attr('checked',false);
    
  • 或在纯 JavaScript 中:

       var ele = document.getElementsByName("Choose");
       for(var i=0;i<ele.length;i++)
          ele[i].checked = false;
    

    JavaScript 演示

回答by Shawn Steward

This should work. Make sure each button has a unique ID. (Replace Choose_Yes and Choose_No with the IDs of your two radio buttons)

这应该有效。确保每个按钮都有唯一的 ID。(用两个单选按钮的 ID 替换 Choose_Yes 和 Choose_No)

document.getElementById("Choose_Yes").checked = false;
document.getElementById("Choose_No").checked = false;

An example of how the radio buttons should be named:

应如何命名单选按钮的示例:

<input type="radio" name="Choose" id="Choose_Yes" value="1" /> Yes
<input type="radio" name="Choose" id="Choose_No" value="2" /> No

回答by shriroop_

If you do not intend to use jQuery, you can use simple javascript like this

如果你不打算使用 jQuery,你可以像这样使用简单的 javascript

document.querySelector('input[name="Choose"]:checked').checked = false;

Only benefit with this is you don't have to use loops for two radio buttons

这样做的唯一好处是您不必为两个单选按钮使用循环

回答by Jasper De Bruijn

Wouldn't a better alternative be to just add a third button ("neither") that will give the same result as none selected?

不是更好的选择是添加第三个按钮(“都不是”),它会给出与没有选择相同的结果吗?

回答by wLc

An ES6 approach to clearing a group of radio buttons:

一种清除一组单选按钮的 ES6 方法:

    Array.from( document.querySelectorAll('input[name="group-name"]:checked'), input => input.checked = false );

回答by Vishnu S

if the id of the radio buttons are 'male' and 'female', value reset can be done by using jquery

如果单选按钮的 id 是 'male' 和 'female',则可以使用 jquery 进行值重置

$('input[id=male]').attr('checked',false);
$('input[id=female]').attr('checked',false);

回答by mahi_0707

If you have a radio button with same id, then you can clear the selection

如果您有一个具有相同 id 的单选按钮,那么您可以清除选择

Radio Button definition :

单选按钮定义:

<input type="radio" name="paymentType" id="paymentType" value="CASH" /> CASH
<input type="radio" name="paymentType" id="paymentType" value="CHEQUE" /> CHEQUE
<input type="radio" name="paymentType" id="paymentType" value="DD" /> DD

Clearing all values of radio button:

清除单选按钮的所有值:

document.formName.paymentType[0].checked = false;
document.formName.paymentType[1].checked = false;
document.formName.paymentType[2].checked = false;
...

回答by Avag Sargsyan

In my case this got the job done:

在我的情况下,这完成了工作:

const chbx = document.getElementsByName("input_name");

for(let i=0; i < chbx.length; i++) {
    chbx[i].checked = false;
}

回答by Diodeus - James MacFarlane

Simple, no jQuery required:

简单,不需要 jQuery:

<a href="javascript:clearChecks('group1')">clear</a>

<script type="text/javascript">
function clearChecks(radioName) {
    var radio = document.form1[radioName]
    for(x=0;x<radio.length;x++) {
        document.form1[radioName][x].checked = false
    }
}

</script>

回答by Salil

YES<input type="radio" name="group1" id="sal" value="YES" >

NO<input type="radio" name="group1" id="sal1" value="NO" >

<input type="button" onclick="document.getElementById('sal').checked=false;document.getElementById('sal1').checked=false">