简体   繁体   English

如何使用复选框jQuery取消选中和刷新数据

[英]How to uncheck and refresh data with checkbox jQuery

I have 4 buttons in this code. 我在这段代码中有4个按钮。 When selecting the "all" button, I would like the other 3 buttons to uncheck and refresh. 选择“全部”按钮时,我希望其他3个按钮取消选中并刷新。 Here is what I have: 这是我有的:

$('div.regions').find('input:checked').each(function () {
    var id = this.id;
    var now = moment();
    if (id == "ALL")
    {
        if (item.Date >= now){ data.push(item); }

    }
    else if (id == item.Region)
    {
        if (item.Date >= now){ data.push(item); }
    }
});

Here is a jsfiddle of the code: http://jsfiddle.net/ZHvYH/ Can anyone help? 这是代码的jsfiddle: http//jsfiddle.net/ZHvYH/任何人都可以帮忙吗?

Updated the JSFiddle - http://jsfiddle.net/ZHvYH/5/ 更新了JSFiddle - http://jsfiddle.net/ZHvYH/5/

var checkboxes = $('div.regions input[type="checkbox"]:not([id="ALL"])');

$('div.regions input[type="checkbox"]').on({
    'change': function(){
        var checkbox = $(this);

        if(checkbox.attr('id') == 'ALL'){
            checkboxes.prop('checked',checkbox.prop('checked')).checkboxradio('refresh');
        }
        else {
            $('#ALL').prop('checked',(checkboxes.length == $('div.regions input[type="checkbox"]:checked:not([id="ALL"])').length)).checkboxradio('refresh');
        }
    }
});

When you check #ALL , remove the checked attribute from all the other checkboxes, and then refresh them. 检查#ALL ,从所有其他复选框中删除checked属性,然后刷新它们。

var checkboxes = $('div.regions input[type="checkbox"]:not([id="ALL"])');

$('#ALL').on({
    'click': function(){
        if (this.checked) {
            checkboxes.removeAttr('checked').checkboxradio("refresh");
        }
    }
});

http://jsfiddle.net/ZHvYH/6/ http://jsfiddle.net/ZHvYH/6/

 if (id == "ALL")
    {
        if (item.Date >= now){ data.push(item); }
    $('input[type=checkbox]').each(function () {
         this.checked ? this.attr('checked', false) : null   ;
    });

    }

Maybe you need a bit of tweaking..i don't tested, i write down as i imagined. 也许你需要一点调整..我没有测试过,我按照我的想象写下来。

hope this helps.. 希望这可以帮助..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM