0

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

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

2 回答 2

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);
    }
});
于 2013-07-26T02:42:39.353 回答
0

你可以试试这个:

$("#checkBox").change(function(){
        if($(this).is(":checked")){
            // if checked   add value
        }else{
            // if unchecked remove value
        }
    });
于 2013-07-26T02:46:41.847 回答