duankua3620 2014-02-27 07:59
浏览 307
已采纳

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

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

  • 写回答

1条回答

  • douxuanling6523 2014-02-27 08:22
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记