Adding items to an object through the .push() method

后端 未结 5 414
Happy的楠姐
Happy的楠姐 2020-12-04 08:15

I\'m doing a loop through few input elements of \'checkbox\' type. After that, I\'m adding values and checked attributes to an array. This is my code:

var st         


        
5条回答
  •  情深已故
    2020-12-04 09:01

    so it's easy)))

    Watch this...

        var stuff = {};
        $('input[type=checkbox]').each(function(i, e) {
            stuff[i] = e.checked;
        });
    

    And you will have:

    Object {0: true, 1: false, 2: false, 3: false}
    

    Or:

    $('input[type=checkbox]').each(function(i, e) {
        stuff['row'+i] = e.checked;
    });
    

    You will have:

    Object {row0: true, row1: false, row2: false, row3: false}
    

    Or:

    $('input[type=checkbox]').each(function(i, e) {
        stuff[e.className+i] = e.checked;
    });
    

    You will have:

    Object {checkbox0: true, checkbox1: false, checkbox2: false, checkbox3: false}
    

提交回复
热议问题