单击使用jquery取消选中单选按钮

时间:2022-11-20 14:31:03

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 fiddle http://jsfiddle.net/8jKJc/220/

我创造了一个小提琴http://jsfiddle.net/8jKJc/220/

But its not working with Bootstrap

但它不能与Bootstrap一起使用

6 个解决方案

#1


14  

Use .prop instead of .attr:

使用.prop代替.attr:

$('input[type="radio"]').prop('checked', false); 

[Demo]

#2


5  

Use this :

用这个 :

 $(this).prop('checked', false);
 //$('input[type="radio"]').attr('checked', false);

You can see this link for 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()和.attr()。 .attr()适用于访问HTML元素属性,如(name,id,class等)和.prop(),用于DOM元素属性,返回大多数情况下的布尔值。

From jQuery official 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.

例如,应检索selectedIndex,tagName,nodeName,nodeType,ownerDocument,defaultChecked和defaultSelected,并使用.prop()方法进行设置。在jQuery 1.6之前,这些属性可以使用.attr()方法检索,但这不在attr的范围内。它们没有相应的属性,只是属性。

#3


2  

It's easy use following will help you:

它很容易使用以下将帮助您:

$("input:checked").removeAttr("checked");

Check your updated Fiddle Here

检查更新的小提琴在这里

#4


2  

If you use jQuery > 1.5, you should use prop instead of attr.

如果你使用jQuery> 1.5,你应该使用prop而不是attr。

$('input[type="radio"]').prop('checked', false);

Demo here.

See here for details.

详情请见此处。

#5


2  

using .prop() inbuild function.

使用.prop()inbuild函数。

$('input[type="radio"]').prop('checked', false);

#6


0  

.prop only sets properties, not attributes.

.prop只设置属性,而不是属性。

jQuery API

Or you can use is to do this

或者你可以使用就是这样做

$("#btn").click(function(){
$("input[type='radio']").each(function() {
if($(this).is(':checked')){
  $(this).prop('checked',false);
   }
});
});

#1


14  

Use .prop instead of .attr:

使用.prop代替.attr:

$('input[type="radio"]').prop('checked', false); 

[Demo]

#2


5  

Use this :

用这个 :

 $(this).prop('checked', false);
 //$('input[type="radio"]').attr('checked', false);

You can see this link for 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()和.attr()。 .attr()适用于访问HTML元素属性,如(name,id,class等)和.prop(),用于DOM元素属性,返回大多数情况下的布尔值。

From jQuery official 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.

例如,应检索selectedIndex,tagName,nodeName,nodeType,ownerDocument,defaultChecked和defaultSelected,并使用.prop()方法进行设置。在jQuery 1.6之前,这些属性可以使用.attr()方法检索,但这不在attr的范围内。它们没有相应的属性,只是属性。

#3


2  

It's easy use following will help you:

它很容易使用以下将帮助您:

$("input:checked").removeAttr("checked");

Check your updated Fiddle Here

检查更新的小提琴在这里

#4


2  

If you use jQuery > 1.5, you should use prop instead of attr.

如果你使用jQuery> 1.5,你应该使用prop而不是attr。

$('input[type="radio"]').prop('checked', false);

Demo here.

See here for details.

详情请见此处。

#5


2  

using .prop() inbuild function.

使用.prop()inbuild函数。

$('input[type="radio"]').prop('checked', false);

#6


0  

.prop only sets properties, not attributes.

.prop只设置属性,而不是属性。

jQuery API

Or you can use is to do this

或者你可以使用就是这样做

$("#btn").click(function(){
$("input[type='radio']").each(function() {
if($(this).is(':checked')){
  $(this).prop('checked',false);
   }
});
});