取消选中radiobutton [英] Uncheck radiobutton

查看:122
本文介绍了取消选中radiobutton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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')
    }
})

这不起作用。我怎样才能使它工作?这是我希望它运行的方式:如果我点击选定的输入,那么这应该是未选中的,但是如果我点击未选择的输入,那么应该检查并取消选中其他输入。

This doesn't work. How can I make it work? Here is how I would like it to run: if I click on selected input, then this should be unselected, but if I click on unselected input, then this should be checked and uncheck the others input.

现场: http://jsfiddle.net/syH46/

推荐答案

我已经想出了这个代码:

I have come up with this code:

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
    };
});

不幸的是,我必须使用额外的变量来存储已检查的属性,因为当点击事件被触发时已选中属性始终为真。

Unfortunately I had to use additional variable to store checked property because when click event is fired checked property is always true.

http://jsfiddle.net/syH46/11/

这篇关于取消选中radiobutton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆