所以我正在构建一个发票工具,我需要能够添加一个新行,因此添加一个新产品,其中包含带有名称和 ID 值的输入。
我有以下 JS,它正在为我完美地创建新行,但是我可以调整任何想法,以便它在输入和选择列表上找到 name="blah"和 id="blah"并添加到末尾 _1, _2、_3 等...每个新行的数字。
JS:
// add new product row on invoice
$(".add-row").click(function () {
$("#invoice_table").each(function () {
var tds = '<tr>';
jQuery.each($('tr:last td', this), function () {
tds += '<td>' + $(this).html() + '</td>';
});
tds += '</tr>';
if ($('tbody', this).length > 0) {
$('tbody', this).append(tds);
} else {
$(this).append(tds);
}
});
return false
});
最佳答案
您可以使用 $.each
... 中可用的索引,例如:
$(".add-row").click(function () {
$("#invoice_table").each(function () {
var tds = '<tr>';
jQuery.each($('tr:last td', this), function (index, element) {
var html = $(this).html().replace(/blah/g, 'blah_'+index);
tds += '<td>' + html + '</td>';
});
tds += '</tr>';
if ($('tbody', this).length > 0) {
$('tbody', this).append(tds);
} else {
$(this).append(tds);
}
});
return false
});
关于javascript - 在 jQuery 中向表中添加新行,但将唯一 ID 应用于输入名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30006154/