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

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

本文关键字:javascript 添加 旋转 html 触摸 调用 函数      更新时间:2023-09-26

我有一个向表中添加新行的按钮。在表格行中有一列带有触摸旋转。我想循环遍历数组(项)。排成一排。但下面的代码使一个错误未捕获类型错误:未定义不是函数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);
    }
}