第二次单击时取消选中单选按钮 [英] Radio button uncheck on second click

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

问题描述

我有很多单选按钮可以从我的数据库中获取一个值,如果它设置为1",我会选中单选按钮.

I have a LOT of radio buttons that grab a value from my database and if it is set to "1", I make the radio button checked.

如果选中了单选按钮,并且用户再次单击它,我仍然希望能够清除此按钮.有人有想法吗?

If a radio button is checked, and a user clicks on it again, I still want to be able to clear this button. Does anyone have an idea?

$radio1 从数据库中获取数据,为 0、1 或 2

$radio1 grabs data from database and will be either 0, 1, or 2

<input value="1" name="radio1" type="radio"<?php if($radio1==1){echo " checked";} ?>>
<input value="2" name="radio2" type="radio"<?php if($radio1==2){echo " checked";} ?>>

Varun Malhotra 的回答稍作修改:我更改了两行对我来说效果更好的代码.但总的来说,Varun 的回答是完美的!

Varun Malhotra's Answer slightly modified: I changed 2 lines of code that worked a little better for me. But overall, Varun's answer was PERFECT!

$('input[type="radio"]').click(function(){
    var $radio = $(this);

    // if this was previously checked
    if ($radio.data('waschecked') == true)
    {
        $radio.prop('checked', false);
        $radio.data('waschecked', false);
    }
    else
    {
         $radio.prop('checked', true);
         $radio.data('waschecked', true);
    }

    // remove was checked from other radios
    $radio.siblings('input[type="radio"]').data('waschecked', false);
});

推荐答案

我建议添加一个自定义属性来跟踪每个收音机的先前状态,如下所示:

I'll suggest to add a custom attribute to keep track of each radio's previous state like so:

$(function(){
    $('input[name="rad"]').click(function(){
        var $radio = $(this);

        // if this was previously checked
        if ($radio.data('waschecked') == true)
        {
            $radio.prop('checked', false);
            $radio.data('waschecked', false);
        }
        else
            $radio.data('waschecked', true);

        // remove was checked from other radios
        $radio.siblings('input[name="rad"]').data('waschecked', false);
    });
});

您还需要将此属性添加到最初检查的单选标记中

You will also need to add this attribute to the initially checked radio markup

<input type="radio" name="rad" id="Radio0" checked="checked" data-waschecked="true" />

JSFIDDLE 演示

更新:

 $(function(){
        $('input[name="rad"]').click(function(){
            var $radio = $(this);

            // if this was previously checked
            if ($radio.data('waschecked') == true)
            {
                $radio.prop('checked', false);
                $radio.data('waschecked', false);
            }
            else
                $radio.data('waschecked', true);

            // remove was checked from other radios
            $radio.siblings('input[type="radio"]').data('waschecked', false);
        });
    });

但请确保您没有其他单选组可以使用,否则您必须提供一些属性来指定这些按钮,例如我之前关注的 name prop.

But do ensure that you don't have other radio-groups to work with, otherwise you have to provide some attributes to specify these buttons like name prop I focused earlier.

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

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