Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

jquery - jQuery-将行追加到两个表(jQuery - append row to two tables)

So, i'm getting some json data and iterating over the result and populating a table based on the resulting values.

(所以,我要获取一些json数据并遍历结果,然后根据结果值填充表格。)

I actually want to populate two tables with the same results and have tried the following code:

(我实际上想用相同的结果填充两个表,并尝试了以下代码:)

$.getJSON( "helpers.php", {
})
.done(function( rows ) {
    $.each(rows, function() {
        // populate the table
        var row = $('<tr>');
        var position = $('<td>').html(this.position);    
        var teamname = $('<td>').html(this.team);
        var points = $('<td>').html(this.points);
        row.append(position,teamname,points);
        $('#table-one').append(row);
        $('#table-two').append(row);  
    });
});

It works if I comment out either of the tables to append, so ie only appending to one or the other, but not both.

(如果我注释掉要附加的任何一个表,则它起作用,即仅附加到一个或另一个,而不是两个都附加。)

Not sure why this doesnt work, but any explanations and code fixes appreciated.

(不知道为什么这不起作用,但是任何解释和代码修复都值得赞赏。)

  ask by translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

See http://api.jquery.com/clone/

(参见http://api.jquery.com/clone/)

You just have to change the last two lines:

(您只需要更改最后两行:)

row.appendTo('#table-one');
row.clone().appendTo('#table-two');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...