Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jQuery清除无线电输入_Javascript_Jquery - Fatal编程技术网

Javascript 使用jQuery清除无线电输入

Javascript 使用jQuery清除无线电输入,javascript,jquery,Javascript,Jquery,我有一堆单选按钮在下面。这些单选按钮是较大表单的一部分,是可选的,因此,如果用户单击一个单选按钮,然后确定他/她不希望选择该选项,则无法撤消此操作 我想知道是否有jQuery等,例如,当单击链接时,根据HTML中的组名称清除任何无线电选择 谢谢 工作演示: 我在init函数中创建了它,然后我调用了init。 } window.onload=Custom.init 我创建了一个类似于roberkules的解决方案,但我的解决方案在选中radiobutton时,如果单击radiobutton本身

我有一堆单选按钮在下面。这些单选按钮是较大表单的一部分,是可选的,因此,如果用户单击一个单选按钮,然后确定他/她不希望选择该选项,则无法撤消此操作

我想知道是否有jQuery等,例如,当单击链接时,根据HTML中的
名称清除任何无线电选择

谢谢

工作演示:

我在init函数中创建了它,然后我调用了init。 }
window.onload=Custom.init

我创建了一个类似于roberkules的解决方案,但我的解决方案在选中radiobutton时,如果单击radiobutton本身,则会清除radiobutton。如果不想在布局中添加额外的“清除”按钮,请使用此选项

用法:对于任何想要以这种方式清除的radiobutton,将其包装在一个类为“RadioClearable”的容器中


通过单击或向radiobutton元素或其标签发送一个键(空格、Escape、Enter、Del、BkSp)来触发代码。

在每个组中都有一个“none of The”选项。查看您在JSFIDLE上提供的JS,现在似乎都可以工作了。谢谢
var group_name = "the_group_name";

// if jquery 1.6++
$(":radio[name='" + group_name + "']").prop('checked', false);

// prev than 1.6
// $(":radio[name='" + group_name + "']").attr('checked', false);
var Custom = {
init: function() {
checkAllPrettyCheckboxes = function(caller, container){

        // Find the label corresponding to each checkbox and click it
        $(container).find('input[type=checkbox]:not(:checked)').each(function(){

            if($.browser.msie){
                $(this).attr('checked','checked');
            }else{
                $(this).trigger('click');
            };
        });
        };

        uncheckAllPrettyCheckboxes = function(caller, container){

            // Find the label corresponding to each checkbox and unselect them
            $(container).find('input[type=checkbox]:checked').each(function(){
                $('label[for="'+$(this).attr('id')+'"]').trigger('click');
                if($.browser.msie){
                    $(this).attr('checked','');
                }else{
                    $(this).trigger('click');
                };
            });
        };
// Requires JQuery 1.4+ (possibly earlier)

$(function () {
    // Turn off a radiobutton if clicked again while on
    var checkOff = function (event) {
        var target = $(event.target);
        if (target.is('label')) {
            // deal with clicked label
            if (target.attr('for')) {
                // label has 'for' attribute
                target = $('#' + target.attr('for'));
            } else {
                // label contains a radiobutton as a child
                target = target.find('input[type=radio]');
            }
        }
        if (target.is('input:checked[type=radio]')) {
            event.preventDefault();
            window.setTimeout(function () {
                target.attr('checked', false);
            }, 200);
        }
    }

    // Find all radiobuttons and labels inside .radio-clearable containers
    $(
        '.radio-clearable input[type=radio], ' +
        '.radio-clearable label').mousedown(function (event) {
        // When clicked -- clear if it was checked
        checkOff(event);
    }).keydown(function (event) {
        // When receiving space, escape, enter, del, or bksp -- clear if it was checked
        if (event.which == 32 || event.which == 27 || event.which == 13 || which == 46 || which == 8) {
            checkOff(event);
        }
    });
});