单选按钮取消选中第二次点击

单选按钮取消选中第二次点击

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

问题描述

我有很多单选按钮可以从我的数据库中获取一个值,如果它设置为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的答案略有修改:
我换了2行我的工作稍微好一点的代码。但总的来说,Varun的答案是PERFECT!

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 DEMO

更新:

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

但请确保您没有其他无线电组合,否则您必须提供一些属性来指定这些按钮,比如我之前关注的名称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.

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

07-24 03:34