2013-03-05 37 views
2

我有这个表:什么方法最好使用JQuery将数据输入到HTML表格中?

 <div id="customer-content"> 
      <table> 
       <caption>Customer Details</caption> 
       <tr> 
        <th>Customer no</th> 
        <td>customer-no</td> 
       </tr> 
       <tr class="alt"> 
        <th>First name</th> 
        <td>first-name</td> 
       </tr> 
       <tr> 
        <th>Last name</th> 
        <td>last-name</td> 
       </tr> 
       <tr class="alt"> 
        <th>Phone number</th> 
        <td>phone-number</td> 
       </tr> 
       <tr> 
        <th>Date of birth</th> 
        <td>dob</td> 
       </tr>     
      </table> 
     </div> 

的是通过jQuery输入一些数据到它的最佳方式:

$('div#customer-content table tr td').text('1234'); 

以上是OK的客户没有,但我怎么做休息?

+0

,将设置所有TD的文本1234 – 2013-03-05 19:36:29

+0

我怎么会那么针对每个单独​​? – 2013-03-05 19:38:45

回答

2

这样做的另一种方式可能是这样的: -

$('div#customer-content table tr td').eq(0).text('1234'); 

这将输入1234到该行的第一个单元格(我们使用零作为第一个项目在JavaScript数组中) 。进入第二,你可以使用: -

$('div#customer-content table tr td').eq(1).text('1234'); 

等等,在EQ选择保值增值,当您去。

Fiddle

+0

非常感谢你,问题解决了。 – 2013-03-05 19:39:25

2
var texts = ['text1','text2','text3',...]; 
var i = 0; 
$('div#customer-content table tr td').each(function(){$(this).text(text[i]);i++;}; 
+0

感谢您的帮助,但我并未尝试将1234设置为每个td单元,而是单独设置。不过谢谢。 – 2013-03-05 19:41:14

+0

好,等等,直到我改变了代码 – MIIB 2013-03-05 19:41:46

+0

好吧,我添加了你可以使用的代码,如果你有n个元素,而不是逐个添加它们 – MIIB 2013-03-05 19:44:03

相关问题