繁体   English   中英

JQuery 多维数组未拾取数组中的 val

[英]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.

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