<html>
<head>
</head>
<script>
function addRow(){
var testTable = document.getElementById("testTable");
var rows = testTable.getElementsByTagName("tr").length;
var newRow = testTable.insertRow(rows);
var cell1 = newRow.insertCell(0);
cell1.innerHTML="<input id='name"+rows+"' type='text' value='computer"+rows+"'>";
var cell2 = newRow.insertCell(1);
cell2.innerHTML="<input id='price"+rows+"' type='text' value='price"+rows+"'>"
}
/**
* 打印数据
*
*/
function printData(){
//获取数据
var data = addData();
//转换成对象
var dataVar = eval('('+data+')');
for(var j=0; j<dataVar.length ; j++){
var values= dataVar[j];
alert("name:"+values.name+"\n"+"price:"+values.price);
}
}
/**
* 装载数据
*
*/
function addData(){
//数据集合
var datas="[";
var trs = testTable.getElementsByTagName("TR");
var rows = testTable.getElementsByTagName("TR").length;
//因为有表头所以从第二行开始是数据
for(var i = 1; i < rows ; i++){
//一对数据
var data ="";
var tr = trs[i];
if(i==1){
data += "{";
}else{
data += ",{";
}
var name = tr.firstChild.firstChild.value;
var price = tr.lastChild.firstChild.value;
data += "name:'"+name+"',price:'"+price+"'}";
datas +=data;
}
datas += "]";
//alert(datas);
return datas;
}
</script>
<body>
<form>
<table id="testTable">
<tr>
<th>笔记本</th>
<th>价格</th>
</tr>
<tr>
<td>
<input id="name0" type="text" value='compute1'>
</td>
<td>
<input id="price0" type="text" value='price1'>
</td>
</tr>
<tr>
<td>
<input id="name1" type="text" value='compute2'>
</td>
<td>
<input id="price1" type="text" value='price2'>
</td>
</tr>
<tr>
<td>
<input id="name2" type="text" value='compute3'>
</td>
<td>
<input id="price2" type="text" value='price3'>
</td>
</tr>
<table>
<input id="1" type="button" value="增加一行" onclick="addRow()"/>
<input id="2" type="button" value="打印数据" onclick="printData()"/>
</form>
</body>
</html>
详细解决方案
js生成Table最好的事例并且获取table json,Copy即可运行
热度:105 发布时间:2012-11-07 09:56:10.0
相关解决方案
- json 解析有关问题
- struts2-json中诠注@JSON(deserialize=false)是什么用?不是阻止JSON反序列化成JAVA对象吗?求解
- 请教哪里可以上到 struts2-json-plugin-2.1.8的源文件
- java+jquery easy ui + json + struts分页例子
- ssh json jar 有关问题
- json 有关问题
- jquery.load(*json) 解析不已。求帮忙
- svn版本有关问题svn:this client is too old to work with working copy.
- jsp页面中加了<table>元素后,页面出现了一大片空白后才出现表格?解决方法
- jsp页面中加了<table>元素后,页面出现了一大片空缺后才出现表格
- Runtime.getRuntime().exec("cmd copy G:\\a\\ C:\\goods\\");没有作用解决办法
- json 转成 string解决办法
- Unknown table 'book' in field list
- 老掉牙的System.arraycopy()有什么地方不如Array.copy
- Copy as active picture 怎么实现应用程序和Word交互
- VS2010 RDLC 报表 表(table) 分页的时候怎么显示表头
- MVC(Json) 出现提示上载
- Table 布局怎么改用CSS定位
- HttpWebRequest post json 数据,接受应该怎么获取啊
- sql清空表数据用truncate table 表名处理,若表名是以数字开头,或者完全是数字,这句会报语法异常
- 大哥们救上命啊ArgumentException: 列“xxx”不属于表 Table
- jqGrid json 不显示数据,该如何处理
- 要取TextBox里的值插入表, insert into table value(); 括号里应该如何写
- 在类名上这样写是啥[Table(Name = "Product")]解决方法
- html中<table>和<asp:Tabel>有什么不一样啊该如何解决
- JQUERY AJAX JSON 有关问题 求大侠指点
- json[0].CodeID值为undefined解决方法
- 发布dll,访问提示异常The located assembly's manifest definition with name 'Copy of TCL" does not match the assembly reference
- 怎么自动补充html中<table>中的宽度和高度
- 关于<table class="Text">