我正在使用 jQuery,我有一组单选按钮,它们的名称都相同,但值属性不同。
例如:
<input type = "radio" name = "thename" value="1"></input>
<input type = "radio" name = "thename" value="2"></input>
<input type = "radio" name = "thename" value="3"></input>
我想把它们全部取消选中。我的页面的当前状态已单击其中之一。我该怎么做?
$("input:radio[name='thename']").each(function(i) {
this.checked = false;
});
不确定为什么 jquery 道具不起作用而这确实...
从 jQuery 1.6 开始,
$("radio").prop("checked", false);
是建议的方法。
这很简单,对我有用。
试试这个:
$('input:radio[name="gender"]').attr('checked',false);
试试这个:
$('input[name="gender"]').prop('checked', false);
@matzahboy 发布的答案非常有效。
尝试过其他方法,但这个方法效果最好:
$(input[name=thename]).removeAttr('checked');
试试下面的代码:
$(input[name=thename]).removeAttr('checked');
这是简单而通用的答案(我相信):
$("input[name=NAME_OF_YOUR_RADIO_GROUP]").prop("checked",false);
对于这个具体问题,我会使用:
$("input[name=thename]").prop("checked",false);
希望这有帮助
对我有用;
$('input[name="radioName"]').attr('checked', false);
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);
});
这允许您双击收音机以重置它们。
要取消选择名为“namegroup”的组的所有无线电,试试这个:
$("input[type=radio][name=namegroup]").prop("checked", false);
`
$('input[name="radio-choices"]').change(function() {
let currentValue =$(this).val();
if (currentValue == 'choices1'){
$("input[name='radio-test-name']").each(function(i) {
this.checked = false;
this.disabled = true;
});
} else {
$("input[name='radio-test-name']").each(function(i) {
this.disabled = false;
});
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<title> Uncheck radio button
</title>
<body>
<input type="radio" name="radio-choices" value="choices2" id="choices2" checked><label for="choices2">Can pick</label>
<input type="radio" name="radio-choices" value="choices1" id="choices1"><label for="choices1">Uncheck and disabled</label>
<br>
<input type="radio" name="radio-test-name" id="test1"><label for="test1">TEST1</label>
<input type="radio" name="radio-test-name" id="test2"><label for="test2">TEST2</label>
<input type="radio" name="radio-test-name" id="test3"><label for="test3">TEST3</label>
</body>
</html>