2016-03-02 61 views
-2

我试图在HTML中创建一个表格,并希望用jQuery来提高它。我怎样才能用JQuery填充表格html

该表是静态的,我希望首先调用“Index”的第一列生成一个带for循环的索引列表。对于视觉像这样:

Indice | champs 1 | champs 2 | champs 3 | 

1 

2 

3 

4 
... 

我的表中有标识id ="balance",我想后,生成表的字段的其余部分。

代码我想:

for (i = 1; i <= 8; i++) { 
 
    $('.bilan td:first-child').append("<td><strong>" + i + "</strong></td>"); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<table class="bilan" cellspacing="0"> 
 
    <tr> 
 
    <td><strong>Indice</strong></td> 
 
    <td><strong>Comptoir</strong></td> 
 
    <td><strong>Nb commandes</strong></td> 
 
    <td><strong>CA (&#x20ac;)</strong></td> 
 
    <td><strong>% CA</strong></td> 
 
    <td><strong>Nouveaux clients</strong></td> 
 
    </tr> 
 
</table>

+0

欢迎SO,你能分享一些代码,你已经尝试了,我们能看到你可能会去错了吗? – Pogrindis

+0

这可能是相关的:http://stackoverflow.com/questions/8749236/create-table-with-jquery-append – Pogrindis

+0

请访问[帮助]和[游览]看看什么和如何问 – mplungjan

回答

0

也许你的意思是

for (i = 1; i <= 8; i++) { 
 
    $(".bilan").append("<tr><td><strong>" + i + "</strong></td></tr>"); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<table class="bilan" cellspacing="0"> 
 
    <tr> 
 
    <td><strong>Indice</strong></td> 
 
    <td><strong>Comptoir</strong></td> 
 
    <td><strong>Nb commandes</strong></td> 
 
    <td><strong>CA (&#x20ac;)</strong></td> 
 
    <td><strong>% CA</strong></td> 
 
    <td><strong>Nouveaux clients</strong></td> 
 
    </tr> 
 
</table>

或更好。

for (i = 1; i <= 8; i++) { 
 
    $(".bilan tbody").append("<tr><td><strong>" + i + "</strong></td></tr>"); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<table class="bilan" cellspacing="0"> 
 
    <thead> 
 
    <tr> 
 
     <th>Indice</th> 
 
     <th>Comptoir</th> 
 
     <th>Nb commandes</th> 
 
     <th>CA (&#x20ac;)</th> 
 
     <th>% CA</th> 
 
     <th>Nouveaux clients</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody></tbody> 
 
</table>

+1

感谢mplungjan它的作品:) –

+0

我该怎么做才能选择我的表中的最后一列来隐藏? –

+0

http://stackoverflow.com/a/5901376/295783 – mplungjan