将数组数据添加到HTML表 [英] Adding array data to HTML Table

查看:182
本文介绍了将数组数据添加到HTML表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将数据数组添加到表格中时出现问题。在firebug中没有显示错误消息,并且数据没有作为行使用 $(#tbNames tr:last)添加到表中。(< tr>< td> + data [0] +< / td>< td>+ data [2] +< / td>< td>< img src ='delete.gif'class ='delete'height = '15'/>< / td>< / tr>);

I have a problem adding the data array into my table. There is no error message shown in the firebug and the data was not added into the table as rows using the $("#tbNames tr:last").after("<tr><td>" + data[0] + "</td><td>" + data[2] + "</td><td><img src='delete.gif' class='delete' height='15' /></td></tr>");.

逻辑(Javascript)

<script type="text/javascript">
var data = [];
data.push("Coco", "Mandy");
data.push("Suzze", "Candy");
data.push("Janny", "Jacky");


$(document).ready(function() {
     $('#btnAdd').live('click', function() {
        var name = $('#txtName').val();
        var name2 = $('#txtName2').val();
        $("#tbNames tr:last").after("<tr><td>" + name + "</td><td>" + name2 + "</td><td><img src='delete.gif' class='delete' height='15' /></td></tr>");
     });

     $('#tbNames td img.delete').live('click', function() {
        $(this).parent().parent().remove();
     });

     $("#insert_data").click(function() {
            for(var i=0; i<data.length; i++){
        $("#tbNames tr:last").after("<tr><td>" + data[0] + "</td><td>" + data[2] + "</td><td><img src='delete.gif' class='delete' height='15' /></td></tr>");
        }
     });      
});
</script>

HTML表格

<input id="txtName" type="text" />
<input id="txtName2" type="text" />
<input id="btnAdd" type="button" value="Add" />
<table id="tbNames" border="1" >
     <tr>
         <th>Name</b></th>
         <th>Name2</b></th>
         <th>Delete</b></th>
      </tr>
      <tr>
         <td>Bingo</td>
         <td>Tingo</td>
         <td><img src="Delete.gif" height="15" class="delete" /></td>
      </tr>
</table>
<input id="insert_data" type="button" style="height: 35px; width: 225px" value="Retrieve Default User" />

请告知我是否遗漏了任何东西。谢谢。

Please advise if I miss out anything. Thanks.

(将插入解决方案文本区域明天我收到此消息后声誉低于100的用户在询问后8小时内无法回答自己的问题。您可以在7小时内自行回答。在此之前请使用评论或编辑您的问题相反。

(Will be insert into the solution text area tomorrow since I got this message Users with less than 100 reputation can't answer their own question for 8 hours after asking. You may self-answer in 7 hours. Until then please use comments, or edit your question instead.

错误1

更改

data.push("Coco", "Mandy"); 
data.push("Suzze", "Candy"); 
data.push("Janny", "Jacky"); 

data.push(["Coco", "Mandy"]); 
data.push(["Suzze", "Candy"]); 
data.push(["Janny", "Jacky"]);

错误2

更改

$("#insert_data").click(function() { 
   for(var i=0; i<data.length; i++){ 
        $("#tbNames tr:last").after("<tr><td>" + data[0] + "</td><td>" + data[2] + "</td><td><img src='delete.gif' class='delete' height='15' /></td></tr>"); 
    } 
}); 

$("#insert_data").click(function() { 
    for(var i=0; i<data.length; i++){ 
        $("#tbNames tr:last").after("<tr><td>" + data[i][0] + "</td><td>" + data[i][1] + "</td><td><img src='delete.gif' class='delete' height='15' /></td></tr>"); 
    } 
});


推荐答案

数据档案包含:['Coco',' Mandy','Suzze','Candy','Janny','Jacky']

The data arrary contains: ['Coco','Mandy','Suzze','Candy','Janny','Jacky']

所以,目前你的代码产生:

So, currently your code produces:

<tr><td>Coco</td><td>Suzze</td><td><img ...etc></td></tr>
<tr><td>Coco</td><td>Suzze</td><td><img ...etc></td></tr>
<tr><td>Coco</td><td>Suzze</td><td><img  ...etc

您的意思是写:

$("#insert_data").click(function() {
  for(var i=0; i<data.length-1; i=i+2){
    $("#tbNames tr:last").after("<tr><td>" + data[i] + "</td><td>" + data[i+1] + "</td><td><img src='delete.gif' class='delete' height='15' /></td></tr>");
    }
});

这篇关于将数组数据添加到HTML表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆