javascript 手動給表增加資料的小例子

來源:互聯網
上載者:User

 先建一個頁面如下:這裡有兩個表,上面一個有資料,下面一個沒有資料,只有一個表頭!
 複製代碼 代碼如下:
 <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>好好學習</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>好好學習</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>好好學習</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>好好學習</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>好好學習</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>

現在點擊新增按鈕,把點中的當前行的資料動態加到下面的TABLE中,javascipt代碼如下:
複製代碼 代碼如下:
<script type="text/javascript">
        function butd(rows) {
            var rows = rows.parentNode.parentNode.rowIndex    //找到當前選中的行
            var mytable = document.getElementById('tables');  //找到當前這個 table;
            var Romm_price = mytable.rows[rows].cells[0].innerText; //找到當前行的第一列的值
            var room_rows = mytable.rows[rows].cells[1].children[0].value; //找到當前行的第二列第一個文字框的值;
            var room_rows2 = mytable.rows[rows].cells[1].children[1].value; //找到當前行的第二列第二個文字框的值;
            var room_rows3 = mytable.rows[rows].cells[2].innerText; //找到當前行的第三列的值;
            //找到table2,並給table2新增一行
            var x = document.getElementById('table2').insertRow();
            x.align = "center";  //設定行樣式
            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>

這個裡面最重要的就是如何得到當前你點擊是哪行,然後是如何手動的把當前行的資料加到table中!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.