且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用jQuery向表中添加新行,并为其增加增量ID

更新时间:2023-11-30 22:24:28

当您可以使用过度设计的版本时,为什么要使用Jason的完全明智的解决方案?

Why use Jason's perfectly sensible solution when you could use an over-engineered version?

; o)

function addRow() {
    var $newRow = $('#gpsTable tbody tr:last-child').clone();
    var newid = $newRow.children().eq(1).find('.StdTableData input').attr('id').match(/\d+/);
    newid = parseInt(newid[0]) + 1;
    var newXid = 'x' + newid;
    var newYid = 'y' + newid;

    $newRow.children().eq(0).find('.StdTableData').text(newid);
    $newRow.children().eq(1).find('.StdTableData input').attr({id:newXid,name:newXid}).val('');
    $newRow.children().eq(2).find('.StdTableData input').attr({id:newYid,name:newYid}).val('');

    $('#gpsTable tbody').append($newRow);
}

哦,以防万一您不太熟悉jQuery,下面的一些代码会将 Sequence (文本)文本标题变成一个按钮,您可以单击该按钮来添加一行.

Oh, and just in case you're not quite so familiar with jQuery, here's some code that will turn your Sequence text heading into a button you can click to add a row.

$(document).ready(function() {  
    $('th:first').click(function() {
        addRow();
    });
});