如何在调用 javascript 函数时将 javascript 触摸旋转添加到 html

How can I add javascript touch spin to html when call javascript function

我有向 table 添加新行的按钮。 在 table 行中有一列带有触摸旋转。 我想遍历 Array(Items)。排成一行。但是下面的代码会导致错误 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);
    }
}