gpt4 book ai didi

javascript - 调用 javascript 函数时如何将 javascript touch spin 添加到 html

转载 作者:行者123 更新时间:2023-11-28 00:27:00 27 4
gpt4 key购买 nike

我有按钮可以将新行添加到表中。在表格行中有一个带有触摸旋转的列。我想循环遍历数组(项目)。排成一排。但下面的代码会产生错误 Uncaught TypeError: undefined is not a function at function tp0

function showtable() {

$('#showtable').html("");

for(var i in Items) {

var no = parseInt($('#tb tr').length) - 1;

var data = "<tr role='row' class='filter' >"
+ "<td>" + no
+ "</td>"
+ "<td>"
+ "<div class='form-group'>"
+ "<input id='touch" + i + "' type='text' value='1' name='touch" + i + "' /> "
+ "<script>"
+ "function tp" + i + " () {$(\"input[name=\'touch" + i + "\']\").TouchSpin(); alert('ttt');}"
+ "</scr" + "ipt>"
+ "</div>"
+ "</td>"
+ "</tr>";

$('#showtable').append(data);

var method_name = "tp";
window[method_name + i]();
}
}

有什么想法谢谢

最佳答案

您应该将行号作为变量传递给预定义的函数,而不是在每一行中添加类似的函数:

function tp(index) {
$("input[name='touch" + index + "']").TouchSpin();
alert('ttt');
}

function showtable() {
$('#showtable').html("");
for (var i in Items) {
var no = parseInt($('#tb tr').length) - 1;

var data = "<tr role='row' class='filter' >"
+ "<td>" + no
+ "</td>"
+ "<td>"
+ "<div class='form-group'>"
+ "<input id='touch"+i+"' type='text' value='1' name='touch"+i+"' /> "
+ "</div>"
+ "</td>"
+ "</tr>";

$('#showtable').append(data);

tp(i);
}
}

关于javascript - 调用 javascript 函数时如何将 javascript touch spin 添加到 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29383752/

27 4 0
文章推荐: html - 如何将
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com