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

How to uncheck and refresh data with checkbox jQuery

本文关键字:刷新 数据 取消 何使用 复选框 jQuery      更新时间:2023-09-26

我在这段代码中有4个按钮。当选择"全部"按钮时,我希望其他3个按钮取消选中并刷新。这是我所拥有的:

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

以下是代码的jsfiddle:http://jsfiddle.net/ZHvYH/有人能帮忙吗?

更新了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');
        }
    }
});

选中#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/

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

也许你需要调整一下。。我没有测试,我按照想象写下来。

希望这能有所帮助。。