简体   繁体   English

JQuery - 使用相同的类循环遍历DIV并为每个类创建数组

[英]JQuery - Loop through DIVs with the same class and create Array for each of them

I do have many DIVs with the same class .list . 我确实有很多同一类的DIV .list They are filled with other divs with the ID element . 它们充满了带有ID element其他div。 In these element divs there a checkboxes and textfields. 在这些element div中有一个复选框和文本字段。 I'm able to get all the checked checkboxes' next Textfield's value. 我能够获得所有选中的复选框'下一个Textfield的值。 I've saved them into one Array but I want an Array for each .list . 我已将它们保存到一个数组中,但我想为每个.list一个数组。

That's how my Code looks so far: 这就是我的代码到目前为止的样子:

function test(){
 var array = new Array();
    $(".list > #element").each(function(){
    array.push($(this).find('input:checkbox[class=unterpunkte]:checked').next('input[type=textfield]').val());
    });
    console.log(array);
}

How can I dynamcially creat an Array for each list. 如何为每个列表动态创建一个数组。 I don't want to have one Array where all the Values are saved. 我不希望有一个数组保存所有值。 I need Arrays for each div with class .list . 我需要使用类.list每个div的数组。 Any ideas? 有任何想法吗?

HTML/PHP: HTML / PHP:

echo("<div class='list' name='$oberpname' value='$oberpname'>");

        while($satz != null)
        {
            echo("<div id='element'><label><input type='checkbox' class='unterpunkte' name='$satz[unterpname]' value='$satz[unterpid]'><input type='textfield' class='$oberpname' value='$satz[unterpname]' readonly/></label></div>");
            $satz=mysql_fetch_assoc($cursor);
        }

        echo("</div>"); //.list div end

EDIT NOTE: Added HTML/PHP 编辑注意:添加了HTML / PHP

Note: ID of an element must be unique, so instead of using element as an ID use it as a class. 注意:元素的ID必须是唯一的,因此不要将element用作ID,而是将其用作类。

You can create an array of arrays, and access the desired list of checkboxes using the index of the list element lik 您可以创建一个数组数组,并使用list element lik的索引访问所需的复选框list

function test() {
    var array = new Array();
    $(".list").each(function () {
        var vals = $(this).find('input:checkbox[class=unterpunkte]:checked').next('input[type=textfield]').map(function () {
            return this.value;
        }).get();
        array.push(vals)
    })
    console.log(array);
}

Now array[0] will give the values for list 1 where as array[1] will give the values for list 2 现在array[0]将给出列表1的值,其中array[1]将给出列表2的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM