javascript 手動給表增加數(shù)據(jù)的小例子
更新時間:2013年07月10日 11:05:18 作者:
這篇文章介紹了js手動給表增加數(shù)據(jù)的實例代碼,有需要的朋友可以參考一下
先建一個頁面如下:這里有兩個表,上面一個有數(shù)據(jù),下面一個沒有數(shù)據(jù),只有一個表頭!
<body>
<form id="form1" runat="server">
<div>
<table border='1px' width="500px" id="tables">
<tr>
<td>1</td><td><input type="text" value="20" style="width:50px" /><input type="text" value="200"style="width:50px" /></td><td>我是中國人</td><td>好好學(xué)習(xí)</td><td><input type="button" id="but" value="新增" onclick="butd(this);" /></td>
</tr>
<tr>
<td>2</td><td><input type="text" value="30"style="width:50px" /><input type="text" value="300"style="width:50px" /></td><td>我是中國人</td><td>好好學(xué)習(xí)</td><td><input type="button" id="Button1" value="新增" onclick="butd(this);" /></td>
</tr>
<tr>
<td>3</td><td><input type="text" value="40" style="width:50px" /><input type="text" value="400" style="width:50px"/></td><td>我是中國人</td><td>好好學(xué)習(xí)</td><td><input type="button" id="Button2" value="新增" onclick="butd(this);" /></td>
</tr>
<tr>
<td>4</td><td><input type="text" value="50" style="width:50px" /><input type="text" value="500"style="width:50px" /></td><td>我是中國人</td><td>好好學(xué)習(xí)</td><td><input type="button" id="Button3" value="新增" onclick="butd(this);" /></td>
</tr>
<tr>
<td>5</td><td><input type="text" value="60" style="width:50px" /><input type="text" value="600"style="width:50px" /></td><td>我是中國人</td><td>好好學(xué)習(xí)</td><td><input type="button" id="Button4" value="新增" onclick="butd(this);" /></td>
</tr>
</table>
</div>
<div>
<table border='1px' width="500px" id="table2">
<tr><td>ID</td><td>年齡</td><td>金錢</td><td>名字</td></tr>
</table>
</div>
</form>
</body>
現(xiàn)在點擊新增按鈕,把點中的當(dāng)前行的數(shù)據(jù)動態(tài)的加到下面的TABLE中,javascipt代碼如下:
<script type="text/javascript">
function butd(rows) {
var rows = rows.parentNode.parentNode.rowIndex //找到當(dāng)前選中的行
var mytable = document.getElementById('tables'); //找到當(dāng)前這個 table;
var Romm_price = mytable.rows[rows].cells[0].innerText; //找到當(dāng)前行的第一列的值
var room_rows = mytable.rows[rows].cells[1].children[0].value; //找到當(dāng)前行的第二列第一個文本框的值;
var room_rows2 = mytable.rows[rows].cells[1].children[1].value; //找到當(dāng)前行的第二列第二個文本框的值;
var room_rows3 = mytable.rows[rows].cells[2].innerText; //找到當(dāng)前行的第三列的值;
//找到table2,并給table2新增一行
var x = document.getElementById('table2').insertRow();
x.align = "center"; //設(shè)置行樣式
var Romm_typename = x.insertCell(0);
var txtDate = x.insertCell(1);
var Guest_Type_ID = x.insertCell(2);
var room_row = x.insertCell(3);
Romm_typename.innerHTML = Romm_price;
txtDate.innerHTML = room_rows;
Guest_Type_ID.innerHTML = room_rows2;
room_row.innerHTML = room_rows3;
}
</script>
這個里面最重要的就是如何得到當(dāng)前你點擊是哪行,然后是如何手動的把當(dāng)前行的數(shù)據(jù)加到table中!
復(fù)制代碼 代碼如下:
<body>
<form id="form1" runat="server">
<div>
<table border='1px' width="500px" id="tables">
<tr>
<td>1</td><td><input type="text" value="20" style="width:50px" /><input type="text" value="200"style="width:50px" /></td><td>我是中國人</td><td>好好學(xué)習(xí)</td><td><input type="button" id="but" value="新增" onclick="butd(this);" /></td>
</tr>
<tr>
<td>2</td><td><input type="text" value="30"style="width:50px" /><input type="text" value="300"style="width:50px" /></td><td>我是中國人</td><td>好好學(xué)習(xí)</td><td><input type="button" id="Button1" value="新增" onclick="butd(this);" /></td>
</tr>
<tr>
<td>3</td><td><input type="text" value="40" style="width:50px" /><input type="text" value="400" style="width:50px"/></td><td>我是中國人</td><td>好好學(xué)習(xí)</td><td><input type="button" id="Button2" value="新增" onclick="butd(this);" /></td>
</tr>
<tr>
<td>4</td><td><input type="text" value="50" style="width:50px" /><input type="text" value="500"style="width:50px" /></td><td>我是中國人</td><td>好好學(xué)習(xí)</td><td><input type="button" id="Button3" value="新增" onclick="butd(this);" /></td>
</tr>
<tr>
<td>5</td><td><input type="text" value="60" style="width:50px" /><input type="text" value="600"style="width:50px" /></td><td>我是中國人</td><td>好好學(xué)習(xí)</td><td><input type="button" id="Button4" value="新增" onclick="butd(this);" /></td>
</tr>
</table>
</div>
<div>
<table border='1px' width="500px" id="table2">
<tr><td>ID</td><td>年齡</td><td>金錢</td><td>名字</td></tr>
</table>
</div>
</form>
</body>
現(xiàn)在點擊新增按鈕,把點中的當(dāng)前行的數(shù)據(jù)動態(tài)的加到下面的TABLE中,javascipt代碼如下:
復(fù)制代碼 代碼如下:
<script type="text/javascript">
function butd(rows) {
var rows = rows.parentNode.parentNode.rowIndex //找到當(dāng)前選中的行
var mytable = document.getElementById('tables'); //找到當(dāng)前這個 table;
var Romm_price = mytable.rows[rows].cells[0].innerText; //找到當(dāng)前行的第一列的值
var room_rows = mytable.rows[rows].cells[1].children[0].value; //找到當(dāng)前行的第二列第一個文本框的值;
var room_rows2 = mytable.rows[rows].cells[1].children[1].value; //找到當(dāng)前行的第二列第二個文本框的值;
var room_rows3 = mytable.rows[rows].cells[2].innerText; //找到當(dāng)前行的第三列的值;
//找到table2,并給table2新增一行
var x = document.getElementById('table2').insertRow();
x.align = "center"; //設(shè)置行樣式
var Romm_typename = x.insertCell(0);
var txtDate = x.insertCell(1);
var Guest_Type_ID = x.insertCell(2);
var room_row = x.insertCell(3);
Romm_typename.innerHTML = Romm_price;
txtDate.innerHTML = room_rows;
Guest_Type_ID.innerHTML = room_rows2;
room_row.innerHTML = room_rows3;
}
</script>
這個里面最重要的就是如何得到當(dāng)前你點擊是哪行,然后是如何手動的把當(dāng)前行的數(shù)據(jù)加到table中!
相關(guān)文章
絕對經(jīng)典的滑輪新聞顯示(javascript+css)實現(xiàn)
這篇文章主要介紹了絕對經(jīng)典的滑輪新聞顯示(javascript+css)實現(xiàn),需要的朋友可以參考下2007-03-03js 靜態(tài)動態(tài)成員 and 信息的封裝和隱藏
一下用面向?qū)ο蟮南嚓P(guān)概念來解釋js中的仿面向?qū)ο?,因為js中不像其他語言,不存在面向?qū)ο笳Z言的相關(guān)特性2011-05-05基于Bootstrap table組件實現(xiàn)多層表頭的實例代碼
Bootstrap table還有一個很多強大的功能,下面就通過本文給大家分享基于Bootstrap table組件實現(xiàn)多層表頭的實例代碼,需要的朋友參考下吧2017-09-09Bootstrap組件學(xué)習(xí)之導(dǎo)航、標(biāo)簽、面包屑導(dǎo)航(精品)
這篇文章主要介紹了Bootstrap組件學(xué)習(xí)之導(dǎo)航、標(biāo)簽、面包屑導(dǎo)航(精品)的相關(guān)資料,需要的朋友可以參考下2016-05-05學(xué)習(xí)JavaScript圖片預(yù)加載模塊
這篇文章主要介紹了js實現(xiàn)圖片預(yù)加載的方法,內(nèi)容很詳細(xì),帶領(lǐng)大家全面認(rèn)識js圖片預(yù)加載模塊,感興趣的小伙伴們可以參考一下2016-11-11