如何通过取消选中框来删除附加值?

时间:2013-07-26 02:35:59

标签: javascript jquery

您好我有复选框以在点击时添加值。但是,即使我取消选中该框,它也会添加。取消选中此框后,如何删除附加值?谢谢!

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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

你可以试试这个:

$("#checkBox").change(function(){
        if($(this).is(":checked")){
            // if checked   add value
        }else{
            // if unchecked remove value
        }
    });