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
Uncheck all JQuery radio buttonset at once
提问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
回答by Frédéric Hamidi
回答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>