javascript 手動(dòng)給表增加數(shù)據(jù)的小例子
更新時(shí)間:2013年07月10日 11:05:18 作者:
這篇文章介紹了js手動(dòng)給表增加數(shù)據(jù)的實(shí)例代碼,有需要的朋友可以參考一下
先建一個(gè)頁面如下:這里有兩個(gè)表,上面一個(gè)有數(shù)據(jù),下面一個(gè)沒有數(shù)據(jù),只有一個(gè)表頭!
<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)在點(diǎn)擊新增按鈕,把點(diǎn)中的當(dāng)前行的數(shù)據(jù)動(dòng)態(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)前這個(gè) 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)前行的第二列第一個(gè)文本框的值;
var room_rows2 = mytable.rows[rows].cells[1].children[1].value; //找到當(dāng)前行的第二列第二個(gè)文本框的值;
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>
這個(gè)里面最重要的就是如何得到當(dāng)前你點(diǎn)擊是哪行,然后是如何手動(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)在點(diǎn)擊新增按鈕,把點(diǎn)中的當(dāng)前行的數(shù)據(jù)動(dòng)態(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)前這個(gè) 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)前行的第二列第一個(gè)文本框的值;
var room_rows2 = mytable.rows[rows].cells[1].children[1].value; //找到當(dāng)前行的第二列第二個(gè)文本框的值;
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>
這個(gè)里面最重要的就是如何得到當(dāng)前你點(diǎn)擊是哪行,然后是如何手動(dòng)的把當(dāng)前行的數(shù)據(jù)加到table中!
相關(guān)文章
絕對(duì)經(jīng)典的滑輪新聞顯示(javascript+css)實(shí)現(xiàn)
這篇文章主要介紹了絕對(duì)經(jīng)典的滑輪新聞顯示(javascript+css)實(shí)現(xiàn),需要的朋友可以參考下2007-03-03js 靜態(tài)動(dòng)態(tài)成員 and 信息的封裝和隱藏
一下用面向?qū)ο蟮南嚓P(guān)概念來解釋js中的仿面向?qū)ο螅驗(yàn)閖s中不像其他語言,不存在面向?qū)ο笳Z言的相關(guān)特性2011-05-05基于Bootstrap table組件實(shí)現(xiàn)多層表頭的實(shí)例代碼
Bootstrap table還有一個(gè)很多強(qiáng)大的功能,下面就通過本文給大家分享基于Bootstrap table組件實(shí)現(xiàn)多層表頭的實(shí)例代碼,需要的朋友參考下吧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實(shí)現(xiàn)圖片預(yù)加載的方法,內(nèi)容很詳細(xì),帶領(lǐng)大家全面認(rèn)識(shí)js圖片預(yù)加載模塊,感興趣的小伙伴們可以參考一下2016-11-11JS如何將秒數(shù)轉(zhuǎn)化為時(shí)分秒的形式
在實(shí)際工作中經(jīng)常會(huì)遇見把秒數(shù)轉(zhuǎn)化為時(shí)分秒的問題,如何處理呢?下面這篇文章主要給大家介紹了關(guān)于JS如何將秒數(shù)轉(zhuǎn)化為時(shí)分秒形式的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12