最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQuery - Loop through DIVs with the same class and create Array for each of them - Stack Overflow

programmeradmin2浏览0评论

I do have many DIVs with the same class .list. They are filled with other divs with the ID element. In these element divs there a checkboxes and textfields. I'm able to get all the checked checkboxes' next Textfield's value. I've saved them into one Array but I want an Array for each .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. Any ideas?

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

I do have many DIVs with the same class .list. They are filled with other divs with the ID element. In these element divs there a checkboxes and textfields. I'm able to get all the checked checkboxes' next Textfield's value. I've saved them into one Array but I want an Array for each .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. Any ideas?

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

Share Improve this question edited Feb 27, 2014 at 8:05 Raviranjan Mishra 8572 gold badges11 silver badges26 bronze badges asked Feb 27, 2014 at 7:59 PreprocezzorPreprocezzor 3071 gold badge6 silver badges17 bronze badges 9
  • please provide the html – Anoop Joshi P Commented Feb 27, 2014 at 7:59
  • 2 IDs must be unique, by definition. – elclanrs Commented Feb 27, 2014 at 8:02
  • @Preprocezzor:you should provide rendered html – Anoop Joshi P Commented Feb 27, 2014 at 8:06
  • Can you explain me the difference between a rendered html and my html? – Preprocezzor Commented Feb 27, 2014 at 8:08
  • textfield is not a valid input type. – elclanrs Commented Feb 27, 2014 at 8:11
 |  Show 4 more ments

1 Answer 1

Reset to default 4

Note: ID of an element must be unique, so instead of using element as an ID use it as a class.

You can create an array of arrays, and access the desired list of checkboxes using the index of the list element lik

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

发布评论

评论列表(0)

  1. 暂无评论