【问题标题】:JQuery Multidimensional Array not picking up val in the arrayJQuery 多维数组未在数组中获取 val
【发布时间】:2020-05-08 02:31:35
【问题描述】:

我有一个 ~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: ""

谢谢你,我很感激你能指导我的任何方向。

【问题讨论】:

    标签: javascript html jquery arrays


    【解决方案1】:

    将您的 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("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);
    

    【讨论】:

    • 如果它混合了复选框和文本可输入字段会是什么样子?
    • 没关系,我想通了。看起来我有多种类型可以找到..(对不起,我是网络开发的菜鸟)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    • 2015-05-10
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多