繁体   English   中英

jQuery将数据数组添加到表行

[英]Jquery add data array to table rows

我有一个Jquery代码来遍历一个表并选择唯一值,然后用这些唯一值填充另一个表。 我的代码如下:

var items = [], options = [];

//Iterate all td's in first column
$('#PartsTable tr td:nth-child(1)').each(function () {
    //add item to array
    items.push($(this).text());
});

//restrict array to unique fleet numbers
var items = $.unique(items);
});

//iterate unique array and build array of select options
// ERROR HERE - 'ITEMS IS UNDEFINED'
$.each(items, function(i, item) {
    options.push('<tr><td align="left" valign="top">' + item + '</td><td class="delete" align="center" style="background-color: transparent;"><i class="fa fa-times text-red cur-pon"></i></td></tr>');
});

//finally append the items from the array
$('#OrderSummaryTbody').append(options.join());

但是我在上面标记的行上遇到了错误-'item is undefined'

当我在Chrome中运行调试器时, var items = $.unique(items); 显然会返回其中包含2个值的数组。

谁能对此有所启示? 谢谢!

这里有错字

var items = $.unique(items);
});

最后一个}); 不应该在这里,或者,它在函数内部,这样项var不是全局的,因此其他代码无法准备它。

如果可以在此之前显示代码以查看完整功能,那将更好。

项定义为其他块。 因此,没有该块您将无法调用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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