Javascript 一次取消选中所有 JQuery 单选按钮集

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

Uncheck all JQuery radio buttonset at once

javascriptjqueryjquery-ui

提问by heron

Using jQ-ui's buttonset feature

使用 jQ-ui 的按钮组功能

<script>
    $(function() {
        $( "#radio" ).buttonset();
    });
    </script>


    <div id="radio">
        <input type="radio" id="radio1" name="radio" /><label for="radio1">Choice 1</label>
        <input type="radio" id="radio2" name="radio" checked="checked" /><label for="radio2">Choice 2</label>
        <input type="radio" id="radio3" name="radio" /><label for="radio3">Choice 3</label>
    </div>

Is there any way to uncheck all radio buttons of buttonset at once?

有没有办法一次取消选中按钮组的所有单选按钮?

回答by Michael Robinson

You can uncheck them them with the following (updated for jQuery UI 1.9:

您可以使用以下命令取消选中它们(针对 jQuery UI 1.9 进行了更新:

$('#radio input').removeAttr('checked');
// Refresh the jQuery UI buttonset.                  
$( "#radio" ).buttonset('refresh');
?

Working JSFiddle.

工作JSFiddle

回答by Frédéric Hamidi

You can match all the radio buttons and use prop()to uncheck them.

您可以匹配所有单选按钮并使用prop()取消选中它们。

However, you also have to refresh the buttonset widget after doing so:

但是,您还必须在执行此操作后刷新按钮组小部件:

$("#radio").find("input:radio").prop("checked", false).end()
           .buttonset("refresh");

回答by Antguider

Before jQuery 1.6 version

jQuery 1.6 版本之前

$(':radio').attr('checked', false);  

OR

或者

$(':radio').removeAttr('checked');

After jQuery 1.6+

jQuery 1.6+ 之后

$(':radio').prop('checked', false);

OR

或者

$(':radio').removeProp('checked');

回答by GDP

Discovered this by accident...with jQuery 1.9.1 using a class name for the buttonset left all the buttons initially unset. Not yet sure if there are ramifications to this, but handy to know.

偶然发现了这一点……使用 jQuery 1.9.1 为按钮集使用类名时,所有按钮最初都未设置。尚不确定这是否有影响,但很容易知道。

$( "div.myclass" ).buttonset();

<div id="myDiv" class="myclass">
    <input type="radio" name="myname" id="id1" value="1"><label for="id1">Label1</label>
    <input type="radio" name="myname" id="id2" value="2"><label for="id2">Label2</label>
    <input type="radio" name="myname" id="id3" value="3"><label for="id3">Label2</label>
</div>

回答by Jaggana

That works for me

这对我行得通

$('input:radio[name="RadioName"]').each(function () { $(this).attr('checked', false); });

回答by Parikshit Sarkar

For JQuery 1.12+

对于 JQuery 1.12+

Wrap the radio buttons within a <div>and give it an id (e.g. "radio")

将单选按钮包裹在 a 中<div>并为其指定一个 id(例如“radio”)

$("#radio").find("input:radio").prop("checked", false)

$("#radio").find("input:radio").prop("checked", false)

回答by Jadli

Javascirpt native way to achieve this

Javascipt 本机方式来实现这一点

function reset(){
//var list = document.querySelectorAll('input[type=radio]');
var list =document.querySelectorAll('input[type="radio"]:checked')
debugger
list.forEach(element => { 
if(element.checked){element.checked=false}}
    
);
}
<div id="myDiv" class="myclass">
    <input type="radio" name="myname" id="id1"  value="1"><label for="id1">Label1</label>
    <input type="radio" name="myname" id="id2" value="2"><label for="id2">Label2</label>
    <input type="radio" name="myname" id="id3" checked value="3"><label for="id3">Label3</label>
</div>

<button onclick="reset()">reset me</button>