您好,我有一个复选框可在点击时添加一个值。但是,即使我取消选中该框,它也会添加。取消选中该框时,如何删除增加的值?谢谢!

$('.addCheckBox').click( function( event ){
                var btn = $(event.currentTarget);
                var Data = btn.data();
                var addList = $('#ListBtn');

                var Obj = {
                    aID: Data.id,
                    aName: Data.name
                };
                addList.push( Obj );
                addListBtn[0].innerHTML = "(" + addList.length + ") in List";
            });

最佳答案

$('.addCheckBox').click(function (event) {
    if ($('.addCheckBox').is(':checked')) {
        var btn = $(event.currentTarget);
        var Data = btn.data();
        var addList = $('#ListBtn');

        var Obj = {
            aID: Data.id,
            aName: Data.name
        };

        addList.push({"myData":Obj});
        console.log(addList);
    }else{
        var addList = $('#ListBtn');
        addList.remove("myData");
        console.log(addList);
    }
});

10-08 04:54