- <html>
- <head runat="server" >
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
- <title>表格相关</title>
- <script language="javascript">
- function add_row(){
- if ((document.getElementById("name").value=="")||(document.getElementById("pass").value==""))
- {alert("值不能为空"); return 0;}
- var tableobj=document.getElementById("table_1");
- var rowobj=tableobj.insertRow(tableobj.rows.length);
- var cell1=rowobj.insertCell(rowobj.cells.length);
- var cell2=rowobj.insertCell(rowobj.cells.length);
- var cell3=rowobj.insertCell(rowobj.cells.length);
- var cell4=rowobj.insertCell(rowobj.cells.length);
- var type = document.getElementsByName("pass")[0];
- var head = type.options[type.selectedIndex].text;
- var passval=document.getElementById("pass").value;
- cell1.innerHTML=document.getElementById("name").value;
- cell2.innerHTML=head;
- cell3.innerHTML=passval;
- cell4.innerHTML="<input type='button' value='删除' onclick='delete_row("+(tableobj.rows.length-1)+");leo()'>";
- document.getElementById("name").value="";
- document.getElementById("pass").value="";
- }
- function leo(){//当删除一行后,对各行重新建立索引!!!
- var tableobj=document.getElementById("table_1");
- for(i=2;i<tableobj.rows.length;i++)
- {
- tableobj.rows[i].cells[2].innerHTML="<input type='button' value='删除' onclick='delete_row("+i+");leo()'>";
- }
- }
- function delete_row(i){
- var tableobj=document.getElementById("table_1");
- alert("删除第"+i+"行");
- tableobj.deleteRow(i);
- }
- function read_row()
- {
- debugger;
- var tableobj=document.getElementById("table_1");
- var str="";
- var cellstr="";
- for(i=2;i<tableobj.rows.length;i++)
- {
- for (j=0;j<tableobj.rows[0].cells.length-1;j++)
- {
- if (str=="")
- {
- str=tableobj.rows[i].cells[j].innerHTML;
- }
- else
- {
- strstr= str+','+tableobj.rows[i].cells[j].innerHTML;
- }
- }
- if (cellstr=="")
- {
- cellstr=str;
- }
- else
- {
- cellstrcellstr= cellstr+';'+str;
- }
- str="";
- }
- alert(cellstr);
- }
- </script>
- </head>
- <body>
- <div align="center">
- <input id="Button1" type="button" value="button" onclick ="read_row();" />
- </div>
- <div align="center">
- <table id="table_1" border="1">
- <tr>
- <td>帐号</td><td>地区</td><td>地区编码</td> <td>操作</td>
- </tr>
- <tr>
- <td style="height: 27px"><input name="name" type="text"> </td>
- <td style="height: 27px"> <select id="pass">
- <option selected="selected" value="1">杭州市</option>
- <option value="2">上城区</option>
- <option value="3">下城区</option>
- <option value="4">江干区</option>
- <option value="5">拱墅区</option>
- <option value="6">西湖区</option>
- <option value="7">萧山区</option>
- <option value="8">余杭区</option>
- <option value="9">桐庐县</option>
- <option value="10">淳安县</option>
- <option value="11">建德市</option>
- <option value="12">富阳市</option>
- <option value="13">临安市</option>
- <option value="14">滨江区</option>
- <option value="15">下沙区</option>
- </select> </td>
- <td style="height: 27px">
- </td>
- <td style="height: 27px"><input name="add" type="button" onclick ="add_row()" value="添加"/> </td>
- </tr>
- </table>
- </div>
- </body>
- </html>
从表格中读写数据的操作,类似于gridview的编辑功能
1,有如下表格,我们要取第2行第2列的值
<table id="table1">
<tr>
<td>1行1列</td>
<td>1行2列</td>
</tr>
<tr>
<td>2行1列</td>
<td>2行2列</td>
</tr>
</table>
2,取值方法
var value==window.table1.rows.item(0).cells.item(0).innerText;
注意:行号,列号是从 0 开始的
例如 var value =window.table1.rows.item(1).cells.item(1).innerText;
这就取到了第2行第2列的值
3.改进方法
上面的方法,需要知道行号,实践中是不方便获得的,下面是改进的方法
<script language="javascript">
var str;
</script>
var str;
</script>
<table >
<tr onMouseDown="str=this.cells.item(0).innerText">
<td>1行1列</td>
<td>1行2列</td>
</tr>
<tr onMouseDown="str=this.cells.item(0).innerText">
<td>2行1列</td>
<td>2行2列</td>
</tr>
</table>
当鼠标按下时,我们就取到了该行第1列的值
<td onMouseDown="str=this.innerText">2行1列</td>
这可以取鼠标所在列的值