使用 jQuery 重置单选按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16099190/
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
Reset radio button using jQuery
提问by Lakshmana Kumar
I'm have multiple radio button. After check or select radio button value randomly, i need to reset radio button to default checked or selected value on button click. My HTML code is below.
我有多个单选按钮。随机检查或选择单选按钮值后,我需要在单击按钮时将单选按钮重置为默认选中或选定的值。我的 HTML 代码如下。
jQuery
jQuery
function ResetRdo() {
// Code to reset ?
}
HTML
HTML
<table>
<tr>
<td>
<input type="radio" name="rdo1" value="a" checked="checked" />
A
<input type="radio" name="rdo1" value="b" />
B
<input type="radio" name="rdo1" value="c" />
C
</td>
</tr>
<tr>
<td>
<input type="radio" name="rdo2" value="1" />
1
<input type="radio" name="rdo2" value="2" checked="checked" />
2
<input type="radio" name="rdo2" value="3" />
3
</td>
</tr>
<tr>
<td>
<input type="radio" name="rdo3" value="1a" />
1a
<input type="radio" name="rdo3" value="2b" />
2b
<input type="radio" name="rdo3" value="3c" checked="checked" />
3c
</td>
</tr>
<tr>
<td>
<input type="button" value="Reset" onclick="ResetRdo()" />
</td>
</tr>
</table>
回答by Joseph Silber
$('input[type=radio]').prop('checked', function () {
return this.getAttribute('checked') == 'checked';
});
Here's the fiddle: http://jsfiddle.net/DNUdv/
这是小提琴:http: //jsfiddle.net/DNUdv/
回答by Anil Shrestha
This might help.
这可能会有所帮助。
$("input:checked").removeAttr("checked");
回答by Harpreet
You can use a unique id for each radio button like:
您可以为每个单选按钮使用唯一的 ID,例如:
<input type="radio" name="rdo1" id="rdo1_a" value="a" checked="checked" />
Now in the function:
现在在函数中:
function ResetRdo() {
$("#rdo1_a").attr('checked') == 'checked';
}
Use the same case for each set.
对每组使用相同的案例。
回答by PSR
You can give id for check boxes then do
您可以为复选框提供 id,然后执行
function reset(){
$("#id").attr('checked', 'checked');
}