3 回答

TA貢獻1783條經(jīng)驗 獲得超4個贊
您需要更改$(this).removeAttr('checked');
為$(this).prop('checked', false);

TA貢獻1790條經(jīng)驗 獲得超9個贊
Please try to this code
$("input[type='radio']").click(function() {
var previousValue = $(this).attr('previousValue');
var name = $(this).attr('name');
if (previousValue == 'checked') {
$(this).prop('checked', false);
$(this).attr('previousValue', false);
} else {
$("input[name=" + name + "]:radio").attr('previousValue', false);
$(this).attr('previousValue', 'checked');
}
});

TA貢獻1876條經(jīng)驗 獲得超5個贊
如果有人試圖取消選中單選按鈕但使用 vanilla js,您可以通過設(shè)置radio.checked為false.
這是一個例子:
<div>
<input type="radio" name="gender" id="male" value="male">
<label for="male">Male</label>
</div>
<div>
<input type="radio" name="gender" id="female" value="female">
<label for="female">Female</label>
</div>
<button id="btnUncheck">Uncheck</button>
香草 js:
document.querySelector('#btnUncheck')
.addEventListener('click', e => {
const radios = document.querySelectorAll('[type="radio"]')
radios.forEach(radio => {
radio.checked = false
})
})
添加回答
舉報