[英]JQuery Multidimensional Array not picking up val in the array
我有一個 ~26 td 的表,它們都是復選框,每個復選框都有一個定義的值。 可能有無限多的行。
以下是其中一個 td 的示例:
<td style="text-align:center"><input type="checkbox" class="largerCheckbox" id="Missing Date" value="true"'+ZMISSDATE+'></td>
我想在 JQuery 中創建一個數組,其中數組將顯示每一行的值。 我完成了數組,但是當我在控制台記錄它時,每行的所有 td 都是空白的。
這是我的 JQuery 創建數組:
var mainArray = [];
$("table#dt-multi-checkbox tr ").each(function(){ /* Loop through all rows of the table(id='dt-multi-checkbox') */
var tempArray = []; /* Declare a temporary array */
$(this).find('td').each(function(){ /* Loop through all elements inside a row */
tempArray.push($(this).val()); /* Use array push() function to add an element to the array */
});
mainArray.push(tempArray);
});
console.log(mainArray);
有人可以告訴我哪里出錯了嗎? 為什么值 ="true 沒有顯示在我的數組中(來自我上面提到的示例 td),我怎樣才能讓它顯示數組中的值?這是我控制台記錄它時的樣子:
Array(26)
0: ""
1: ""
2: ""
3: ""
4: ""
5: ""
6: ""
7: ""
8: ""
9: ""
10: ""
11: ""
12: ""
13: ""
14: ""
15: ""
16: ""
17: ""
18: ""
19: ""
20: ""
21: ""
22: ""
23: ""
24: ""
25: ""
謝謝你,我很感激你能指導我的任何方向。
將您的 jquery function 更改為:
var mainArray = [];
$("table#dt-multi-checkbox tr ").each(function(){ /* Loop through all rows of the table(id='dt-multi-checkbox') */
var tempArray = []; /* Declare a temporary array */
$(this).find("input[type='checkbox']").each(function(){ /* Loop through all elements inside a row */
tempArray.push($(this).val()); /* Use array push() function to add an element to the array */
});
mainArray.push(tempArray);
});
console.log(mainArray);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.