2016-03-02 34 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