0

嘿,我正试图找出一种方法,当我循环查看页面上的哪些复选框被选中时,将数据添加到 JS 对象:

jQuery('#doaction').click(function () {
    var bulkSelected = jQuery('#bulkAction').val();
    var checkCount = 1;

    if (bulkSelected == 0) {
        console.log('no bulk selected category');
        jQuery('#overlay-shade').fadeOut(300);
    } else {
        var doBulk = {
            called: '' + bulkSelected + '',
        };

        jQuery('input[type=checkbox]').each(function () {                           
            if (jQuery(this).attr('id') != 'selectAll') {
                var isChecked = (this.checked ? true : false);

                if (isChecked == true) {
                    console.log(jQuery(this).attr('id'));
                    console.log(jQuery(this).attr('data-accnum'));
                }
                checkCount++;
            }
        });

    }

.attr('data-accnum')的数字如下所示。我需要它采用这种格式:

(复选框名称:acnum)

var doBulk = {
    chk1: 52136523168,
    chk2: 54298851631
    ect..ect...
};

我怎样才能做到这一点?

4

2 回答 2

1

尝试:

var doBulk = {};
jQuery('input[type=checkbox]').each(function () {                           
    if (jQuery(this).attr('id') != 'selectAll') {
        var isChecked = (this.checked ? true : false);

        if (isChecked == true) {
            doBulk[jQuery(this).attr('id')] = +jQuery(this).attr("data-accnum");
        }
        checkCount++;
    }
});
console.log(doBulk);
于 2013-04-30T01:15:21.027 回答
0

jQuery has a :checked selector, as well as a not method which we use to only get all checked inputs that are not #selectAll.

Then you just add it to the doBulk object using bracket notation, accessing the doBulk object from within the each function is possible because javascript has closures.

    var doBulk = {}
      , checked = jQuery('input[type=checkbox]:checked').not('#selectAll')
      , checkCount = checked.length;

    checked.each(function () {                           
      doBulk[this.id] = jQuery(this).attr('data-accnum');
    });
于 2013-04-30T01:34:30.310 回答