![](/img/trans.png)
[英]Jquery: Loop through each sibling and give each of them a different class?
[英]JQuery - Loop through DIVs with the same class and create Array for each of them
我確實有很多同一類的DIV .list
。 它們充滿了帶有ID element
其他div。 在這些element
div中有一個復選框和文本字段。 我能夠獲得所有選中的復選框'下一個Textfield的值。 我已將它們保存到一個數組中,但我想為每個.list
一個數組。
這就是我的代碼到目前為止的樣子:
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);
}
如何為每個列表動態創建一個數組。 我不希望有一個數組保存所有值。 我需要使用類.list
每個div的數組。 有任何想法嗎?
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
編輯注意:添加了HTML / PHP
注意:元素的ID必須是唯一的,因此不要將element
用作ID,而是將其用作類。
您可以創建一個數組數組,並使用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);
}
現在array[0]
將給出列表1的值,其中array[1]
將給出列表2的值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.