Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用jQuery读取动态html表行_Javascript_Jquery_Html - Fatal编程技术网

Javascript 如何使用jQuery读取动态html表行

Javascript 如何使用jQuery读取动态html表行,javascript,jquery,html,Javascript,Jquery,Html,我已经尝试了上面的代码,但是$.trim(row.find(“.itemName”).html()和$.trim(row.find(.itemQty”).html()都返回了空字符串 有人知道我如何成功地将表行读入arrItems变量吗 下表为html var arrItems = []; $('#myDynamicTable').find('tr').each(function () { var row = $(this); var item = new GatePassIt

我已经尝试了上面的代码,但是
$.trim(row.find(“.itemName”).html()和
$.trim(row.find(.itemQty”).html()都返回了空字符串

有人知道我如何成功地将表行读入
arrItems
变量吗

下表为html

 var arrItems = [];
$('#myDynamicTable').find('tr').each(function () {
    var row = $(this);
    var item = new GatePassItemsViewModel("", "", $.trim(row.find(".itemName").html()), $.trim(row.find(".itemQty").html()));
    arrItems.push(item);
});

项目
量

我不知道为什么您的
.html()
调用没有返回任何内容,但我可以看到出现问题的一些原因

主要问题是
row.find(“.itemName”).html()
将/应该返回表单元格包含的html,即
,而您需要的是该输入元素的值。因此,对于这两种情况,都更改为
行.find(“.itemName input”).val()
(即,以输入元素为目标并读取其值),您的状态应该会更好

要验证这些值,只需执行以下操作:

 <table id="myDynamicTable" class="displaynone">
                                    <tbody>
                                        <tr>
                                            <th style='width:220px;'><b>Item</b></th>
                                            <th style='width:15px;'><b>Quantity</b></th>
                                        </tr>
                                    </tbody>
                                </table>
这将测试修剪后,
项目
的长度是否大于零,以及
数量
是否为整数(仅由一个或多个数字组成)

其他问题/评论:

  • AddRow()
    中,您的表格单元格以某种方式嵌套。您需要先关闭第一个单元格,然后再打开第二个单元格(并移除最后一个双精度
  • $.trim(row.find(“.searchFirstName”).html())
    不会产生任何效果,因为它只返回修剪后的值,不会更改要修剪的变量或类似值

这是可行的,现在唯一的问题是它也在读取表格标题,如何跳过标题?@SQL.NETWarrior:将您的
标记放在
下,并将自定义行放在
内。然后添加新行,如
$('#myDynamicTable')。find('tbody tr')
 <table id="myDynamicTable" class="displaynone">
                                    <tbody>
                                        <tr>
                                            <th style='width:220px;'><b>Item</b></th>
                                            <th style='width:15px;'><b>Quantity</b></th>
                                        </tr>
                                    </tbody>
                                </table>
...
var row = $(this),
  itemText = $.trim(row.find(".itemName input").val()),
  qty= $.trim(row.find(".itemQty input").val());

if(itemText.length === 0 || !qty.match(/\d+/)) {
  //Handle the problem of invalidity
}