javascript 手動(dòng)給表增加數(shù)據(jù)的小例子
更新時(shí)間:2013年07月10日 11:05:18 作者:
這篇文章介紹了js手動(dòng)給表增加數(shù)據(jù)的實(shí)例代碼,有需要的朋友可以參考一下
先建一個(gè)頁(yè)面如下:這里有兩個(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>我是中國(guó)人</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>我是中國(guó)人</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>我是中國(guó)人</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>我是中國(guó)人</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>我是中國(guó)人</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>我是中國(guó)人</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>我是中國(guó)人</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>我是中國(guó)人</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>我是中國(guó)人</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>我是中國(guó)人</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)文章
?JavaScript?數(shù)據(jù)結(jié)構(gòu)之散列表的創(chuàng)建(2)
這篇文章主要介紹了?JavaScript?數(shù)據(jù)結(jié)構(gòu)之散列表的創(chuàng)建,主要看如何處理散列值沖突的問題,并實(shí)現(xiàn)更完美的散列表。下文詳細(xì)介紹需要的小伙伴可以參考一下2022-04-04
javascript實(shí)現(xiàn)富文本框選中對(duì)齊的思路與代碼
最近在項(xiàng)目中經(jīng)常遇到使用富文本框的情況,下面這篇文章主要給大家介紹了關(guān)于javascript實(shí)現(xiàn)富文本框選中對(duì)齊的思路與代碼,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03
JS基于FileSystemObject創(chuàng)建一個(gè)指定路徑的TXT文本文件
這篇文章主要介紹了JS基于FileSystemObject創(chuàng)建一個(gè)指定路徑的TXT文本文件,涉及javascript使用ActiveXObject控件中FileSystemObject對(duì)象模型的基本技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
微信瀏覽器內(nèi)置JavaScript對(duì)象WeixinJSBridge使用實(shí)例
這篇文章主要介紹了微信瀏覽器內(nèi)置JavaScript對(duì)象WeixinJSBridge使用實(shí)例,本文給出了分享到朋友圈、發(fā)送給好友、分享到騰訊微博、關(guān)注指定的微信號(hào)等功能代碼,需要的朋友可以參考下2015-05-05
也說JavaScript中String類的replace函數(shù)
最近讀了sharpxiajun的博文《javascript筆記--String類replace函數(shù)的一些事》,感覺寫的很好,很有幫助。2011-09-09

