使用 jquery 在单击时取消选中单选按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32178646/
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 radio button on click using jquery
提问by DON
Radio buttons are unchecked only at page refresh
仅在页面刷新时取消选中单选按钮
<input type="radio" name="test">
1<input type="radio" name="test">2
<input type="button" id="btn" />
$("#btn").click(function(){
$(':radio').each(function () {
$(this).removeAttr('checked');
$('input[type="radio"]').attr('checked', false);
})
});
I have created a fiddlehttp://jsfiddle.net/8jKJc/220/
我创建了一个小提琴http://jsfiddle.net/8jKJc/220/
But its not working with Bootstrap
但它不适用于 Bootstrap
回答by Mohit Kumar
回答by Norlihazmey Ghazali
Use this :
用这个 :
$(this).prop('checked', false);
//$('input[type="radio"]').attr('checked', false);
You can see this linkfor differentiate between .prop()
with .attr()
. .attr()
suitable for accessing HTML element attributes like(name, id, class,etc..)
and .prop()
for DOM element properties that returned boolean value for most case.
您可以查看此链接以区分.prop()
with .attr()
。.attr()
适合于存取HTML元素属性等(name, id, class,etc..)
和.prop()
对,对大多数情况下返回布尔值的DOM元素的属性。
From jQueryofficial page :
来自jQuery官方页面:
For example, selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, and defaultSelected should be retrieved and set with the .prop() method. Prior to jQuery 1.6, these properties were retrievable with the .attr() method, but this was not within the scope of attr. These do not have corresponding attributes and are only properties.
例如,应该使用 .prop() 方法检索和设置 selectedIndex、tagName、nodeName、nodeType、ownerDocument、defaultChecked 和 defaultSelected。在 jQuery 1.6 之前,这些属性可以使用 .attr() 方法检索,但这不在 attr 的范围内。这些没有相应的属性,只是属性。
回答by Norlihazmey Ghazali
using .prop() inbuild function.
使用 .prop() inbuild 函数。
$('input[type="radio"]').prop('checked', false);
回答by ketan
It's easy use following will help you:
它易于使用,以下将帮助您:
$("input:checked").removeAttr("checked");
Check your updated Fiddle Here
回答by Andrea Salicetti
回答by Varun
.prop
only sets properties, not attributes.
.prop
只设置属性,不设置属性。
Or you can use is
to do this
或者你可以is
用来做这个
$("#btn").click(function(){
$("input[type='radio']").each(function() {
if($(this).is(':checked')){
$(this).prop('checked',false);
}
});
});