Jquery插入表行和增量输入id

时间:2014-08-22 23:54:26

标签: javascript php jquery html forms

我有一个动态html表/表单,有两个按钮可以向表中添加一行。一个按钮在表的末尾添加一行,并适当增加输入ID。另一个按钮用于在当前行之前插入一行...它可以执行,直到我尝试使其增加id。我当前的jquery代码适当地更新了id,但删除了标签,导致所有输入字段被粉碎到表的单个单元格中。我已经在下面提供了我现有的代码段。 。 。我错过了什么?

link to fiddle

jquery代码:

$(document).on('click', 'button.insertbutton', function () {
    var rowCount = $('#orderDetail tr').length;
    alert(rowCount);
    $(this).closest('tr').before($(this).closest('tr').find('input')
        .each(function() {
        $(this).attr({
          'id':  function(_, id) { 
            var regex = /[a-zA-Z]+/
            return id.match(regex) + rowCount },
          //'name':  function(_, name) { return name + rowCount },
           'value': '',
        });
      })
    ).clone(true);
    return true;
});

我怀疑问题是在迭代输入项目然后克隆该行的部分内。

非常感谢任何指导。

根据推荐编辑包含的内容......感谢您的推荐。

1 个答案:

答案 0 :(得分:0)

我能够通过将操作分解为两个步骤而不是尝试在一行中完成任务来解决克隆和增加ID。 insert和update id的jquery代码如下:

$(document).on('click', 'button.insertbutton', function () {
var rowCount = $('#orderDetail tr').length;
$(this).closest('tr').before($(this).closest('tr').clone());
$(this).closest('tr').find('input').each(function() {
    $(this).attr({
        'id': function(_,id) {
            var regex = /[a-zA-Z]+/
            return id.match(regex) + rowCount },
        'value': '',
    })
})

return true;

});

我希望这有助于将来的某个人

相关问题