<input type="radio" name="things" value="exam" /> exam
<input type="radio" name="things" value="journey" /> journey
$("input[name=things]").click(function(){
if($(this).is(':checked')){
$(this).removeAttr('checked')
}
})
这行不通。我怎样才能让它发挥作用?以下是我希望它运行的方式:如果我单击选定的输入,则应取消选中它,但如果我单击未选定的输入,则应选中它并取消选中其他输入。
最佳答案
我想出了这个代码:
var prev = {};
$("input[name=things]").click(function(){
if (prev && prev.value == this.value) {
$(this).prop('checked', !prev.status);
}
prev = {
value: this.value,
status: this.checked
};
});
不幸的是,我不得不使用额外的变量来存储选中的属性,因为当点击事件被触发时,checked
属性总是为真。
关于javascript - 取消选中单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9480862/