【问题标题】:Clearing/Un-selecting Radio button on second click第二次单击时清除/取消选择单选按钮
【发布时间】:2014-01-21 18:05:02
【问题描述】:

我正在创建的表单上有很多单选按钮。

我使用单选按钮而不是复选框的原因是因为我只想让用户选择一个或不选择。

单选按钮的坏处是,一旦被选中,就无法撤消。我正在尝试添加在第二次单击时清除一组单选框的功能。

如果我有一组我想单独定位的单选按钮,但如果我想为页面上的所有单选按钮组实现它,我现在正在实施的方法有效。

当我使用多个单选按钮组时,当我尝试选择不同的选项时,它会随机取消选择单选按钮。

如果有人可以帮助我,将不胜感激。

JSFIDDLE for only one group of radio buttons(有效)

如果您改为改用此代码并以个人名称为目标,它会起作用。

$('input[name="rad"]').click(function()

JSFIDDLE for multiple groups of radio buttons(不起作用)

我正试图同时定位我所有的单选按钮组,因为有很多。

$(function(){
    $('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.data('waschecked', true);

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

【问题讨论】:

  • 您希望用户能够选择一个或无,请使用复选框。选中一个复选框时禁用其他复选框,取消选中时启用它们。
  • 如果他们选择 2? @tymeJV
  • 收听单选按钮时,您需要收听change 事件,因为click 在整个网络浏览器范围内充其量是不一致的

标签: javascript jquery html radio-button


【解决方案1】:

因此,您只需要一种方法来可靠地通过name 组进行检查/取消检查。

这样的事情应该可以工作:

$('input[type="radio"]').on('click change', function () {//on click/change    
    $(this).prop('checked')//if checked
    ? $(this).prop('checked',false).data('waschecked', false)//uncheck
    : $(this).prop('checked',true).data('waschecked', true)//else check
    .siblings('input[name="'+$(this).prop('name')+'"]').data('waschecked', false);//make siblings false
});

保留.data('waschecked', ...) 以防您需要该值,但没有它也可以这样工作:

$('input[type="radio"]').on('click change', function () {//on click/change    
    $(this).prop('checked')//if checked
    ? $(this).prop('checked',false)//uncheck
    : $(this).prop('checked',true);//else check
});

做了一个小提琴:http://jsfiddle.net/filever10/ZUb65/

【讨论】:

  • 这很完美!谢谢你。这应该适用于大多数浏览器吗?
  • 确实应该。如果您发现其中一个不起作用,请告诉我,我会解决的。
【解决方案2】:

尝试更改行以在选择器表达式中使用单击单选的名称属性

$radio.siblings('input[name='+ $(this).attr('name') +']').data('waschecked', false);

您只需要清除同一组其他收音机上的“已检查”标志。

http://jsfiddle.net/jammykam/BtLxY/15/

以防万一您确实需要使用 onchange 进行定位:http://jsfiddle.net/jammykam/BtLxY/27/

$('input[type="radio"]').on('change', 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='+ $(this).attr('name') +']').data('waschecked', false);
});

【讨论】:

  • 嘿 Jammy,这在 jsfiddle 中完美运行。但由于某种原因,我的网站无法正常工作。不知道为什么。它可能与我使用自定义图像作为我选择的单选按钮有关。我得看看。感谢您的帮助。
【解决方案3】:

如果您使用的是简单的主要到次要关系,那么试试这个:

$('input[type="radio"][name="main_radios"]').on('change', function(){

    $('input[type="radio"][name="secondary_radios"]').prop('checked', false);
    $('input[type="radio"][name="third_radios"]').prop('checked', false);
    $('input[type="radio"][name="fourth_radios"]').prop('checked', false);

    // combine it all into one call if you really feel like it
    $('input[type="radio"][name="secondary_radios"], input[type="radio"][name="third_radios"], input[type="radio"][name="fourth_radios"]').prop('checked', false);

});

取消选中所有不是“这个”名称的收音机

// Listen for ANY radio to be changed
$('input[type="radio"]').on('change', function(){

    // remember name of selected radio
    var checked_name = $(this).attr('name');

    // target only radios that are not "this" name and uncheck them
    $('input[type="radio"]').filter(function(){
        if($(this).attr('name') != checked_name){
            return true;
        }
        else{
            return false;
        }
    }).prop('checked', false);

});

【讨论】:

  • 假设我有 100 组不同的单选按钮。不确定我是否要为每 100 个按钮创建一个新功能。
猜你喜欢
  • 2013-11-22
  • 2016-12-10
  • 1970-01-01
  • 1970-01-01
  • 2019-07-27
  • 1970-01-01
  • 2018-01-07
  • 2011-02-10
  • 1970-01-01
相关资源
最近更新 更多