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

标签 javascript jquery html twitter-bootstrap-3

我有按钮可以将新行添加到表中。 在表格行中有一个带有触摸旋转的列。 我想循环遍历数组(项目)。排成一排。但下面的代码会产生错误 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/

相关文章:

javascript - Redux-Thunk Chaining Actions,得到一个“dispatch(...) then is not a function error

javascript - 为弹出 div 以外的屏幕设置黑色背景

javascript - Javascript更改音频src

javascript - 如何在 Owl Carousel 元素之间添加空格

javascript - 如何在表单中保存 "add another"数据?

javascript - 使用悬停将图像加载到另一个图像上 (Javascript)

javascript - 在重新加载时删除标记时,react-native-maps 中的 callout View 不会消失

javascript - jquery按钮点击问题

javascript - 提交时如何使单选字段确定下一页

javascript - 如何从 Javascript 函数执行 Angular 函数?