`

js生成Table最好的例子并且获取table json,Copy即可运行

    博客分类:
  • JS
阅读更多
<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组件Bootstrap Table使用方法详解

    bootStrap table获取数据有两种方式,一是通过table 的data-url属性指定数据源,二是通过JavaScript初始化表格时指定url来获取数据 [xhtml] view plain copy &lt;table data-toggle="table"&gt; &lt;thead&gt; ... ...

    jsp KindEditor网页文本编辑器

    放在项目的跟路径即可,下面是初始化代码,编辑器没有实例化显示出来,注意引入的js路径 [removed][removed] [removed][removed] &lt;form action="#"&gt; &lt;textarea id="editor1" name="editor1" &gt;&lt;/...

    CKFMDBHelper

    对FMDB的封装,以面向对象的方式操作数据库,并且对服务器model的支持,模型、字典、json相互转换,并提供字段映射 ##如何使用 ###Model创建 #import "CKModel.h" @interface CKTestModel : CKModel @property ...

    jquery插件使用方法大全

    老的core.js文件被分为attribute.js, css.js, data.js, manipulation.js, traversing.js和queue.js;CSS和attribute的逻辑分离。 jQuery 1.5(2011年1月31日):该版本修复了83个bug,解决了460个问题。重大改进有:...

    mysql-8-cookbook2018

    to MySQL 8, such as the JSON datatype, common table expressions, and window functions. Chapter 4, Configuring MySQL, shows you how to configure MySQL and basic configuration parameters. Chapter 5, ...

    PHP.and.MySQL.Recipes.A.Problem-Solution.Approach.1484206061

    First, this book is a source of instant solutions, including countless pieces of useful code that you can just copy and paste into your own applications, giving you answers fast and saving you hours ...

    xheditor-1.1.14

    参数:可定义多个JSON对象数组,示例如下:{qq:{name:'QQ',count:55,width:25,height:25,line:11},msn:{name:'MSN',count:40,width:22,height:22,line:8}} name:表情分组名 count:表情数量 list:表情列表,例...

    详解iOS tableViewCell自适应高度 第三发类库

    在github中有许多大牛封装好的第三发类库,其中有个自适应cell高度的类库 ...model类 ...@property(nonatomic,copy)NSArray&lt;getCommentData&gt; *commentList; @end #import commentsModel.h @implementati

    go系统编程(英文版)

    it shows Go implementations of a linked list, a binary tree and a hash table. After that, it discusses Go packages and teaches you how to create and use your own Go packages. The last part of the ...

    ICS delphixe10源码版

    Table of content: ----------------- - Legal issues - Donate - Register - Contributions - Latest Versions - Version Control repository - Installation - Available VCL Components - Sample applications -...

Global site tag (gtag.js) - Google Analytics