简体   繁体   English

如何使用jQuery在表格单元中动态添加值?

[英]How to add value dynamically in table cell using jQuery?

For example I have an table filled with values 例如,我有一个充满值的表

-----------------------------------------
S.No   |   Name      |       Address
-----------------------------------------
       |  Dinesh     |       Salem
-----------------------------------------
       |  Senthil    |       Chennai
-----------------------------------------
       |  Sundar     |       Namakkal
-----------------------------------------

Actually I need to fill serial no to each row dynamically using jQuery. 实际上,我需要使用jQuery向每行动态填充serial no

var i = 1;
$('#tbl td').each(function() {
    $(this).append(i);
    i++;
});

I need the output should be: 我需要的输出应该是:

-----------------------------------------
S.No    |   Name      |       Address
-----------------------------------------
   1    |  Dinesh     |       Salem
-----------------------------------------
   2    |  Senthil    |       Chennai
-----------------------------------------
   3    |  Sundar     |       Namakkal
-----------------------------------------

Please help me to solve this problem. 请帮我解决这个问题。

If you wanna replace first td , you can use :first-child 如果要替换first td,可以使用:first-child

Try like this 这样尝试

var i=1;
$('#tbl td:first-child').each(function() {
    $(this).html(i);
    i++;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM