使用 jQuery 取消选择单选按钮组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7044301/
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
Unselect group of radio buttons using jQuery
提问by Carl
I'm using jQuery and I have a group of radio buttons all with the same name, but different value properties.
我正在使用 jQuery 并且我有一组单选按钮,它们都具有相同的名称,但具有不同的值属性。
For example:
例如:
<input type = "radio" name = "thename" value="1"></input>
<input type = "radio" name = "thename" value="2"></input>
<input type = "radio" name = "thename" value="3"></input>
I want to make it so they are all unselected. Current state of my page has one of them clicked. How do I do this?
我想让它们都未被选中。我的页面的当前状态已单击其中之一。我该怎么做呢?
回答by Dave Kiss
As of jQuery 1.6, $("radio").prop("checked", false);
is the suggested method.
从 jQuery 1.6 开始,$("radio").prop("checked", false);
是建议的方法。
回答by Bogdan M
$("input:radio[name='thename']").each(function(i) {
this.checked = false;
});
not sure why the jquery prop doesn't work and this does...
不知道为什么 jquery 道具不起作用,这确实......
回答by Kyle
回答by viniciusalvess
The answer posted by @matzahboy worked perfectly.
@matzahboy 发布的答案效果很好。
Tried other ways but this one worked the best:
尝试了其他方法,但这个方法效果最好:
$(input[name=thename]).removeAttr('checked');
回答by matzahboy
Try the following code:
试试下面的代码:
$(input[name=thename]).removeAttr('checked');
回答by JimmyH
This is the simple and generic answer (I believe):
$("input[name=NAME_OF_YOUR_RADIO_GROUP]").prop("checked",false);
这是简单而通用的答案(我相信):
$("input[name=NAME_OF_YOUR_RADIO_GROUP]").prop("checked",false);
For this specific question, I would use:
对于这个特定问题,我会使用:
$("input[name=thename]").prop("checked",false);
$("input[name=thename]").prop("checked",false);
Hope this helps
希望这可以帮助
回答by Baqer Naqvi
it worked for me;
它对我有用;
$('input[name="radioName"]').attr('checked', false);
回答by Rakhal Raj Mandal
This is simple and works for me.
这很简单,对我有用。
Try this one:
试试这个:
$('input:radio[name="gender"]').attr('checked',false);
Try this one:
试试这个:
$('input[name="gender"]').prop('checked', false);
回答by Pablo Uribe A.
function resetRadio(name) {
$('#form input:radio[name=' + name + ']:checked').each(function () {
var $this = $(this);
$this.prop("checked", false);
});
}
$('#form input:radio').on('dblclick', function () {
var $this = $(this);
var name = $this.prop('name');
resetRadio(name);
});
This allows you to double click the radios to reset them.
这允许您双击无线电来重置它们。
回答by nonide
To unselect all the radios of a group called "namegroup", try this:
要取消选择名为“namegroup”的组的所有收音机,请尝试以下操作:
$("input[type=radio][name=namegroup]").prop("checked", false);