js如何将表单中数据添加到表格中

 时间:2024-10-03 14:28:47

网站开发常常离不开表格的使用,如何使用JavaScript将表单的数据添加到表格中去呢?

方法/步骤

1、这里用一个例子来说明耦嘀扔硇怎么添加数据。首先新建一个空白的html文件,添加表单数据:三个文本框,一个提交按钮,一个表格。<b泠贾高框ody> <formonsubmit="returnsubmitTable()"> 账号:<inputtype="text"id="account"><br> 姓名:<inputtype="text"id="name"><br> 密码:<inputtype="password"id="password"><br> <inputname="submit"type="button"id="submit"value="提交"onclick="returnsubmitTable()"> </form> <hr> <tableborder="1"width="50%"align="center"id="table"> <tr> <th>账号</th><th>姓名</th><th>密码</th> </tr> </table></body>可以直接复制上面的代码到编辑器中测试。效果如图所示:

js如何将表单中数据添加到表格中

2、JavaScript的代码:<scripttype="text/javascript"> functionsubmitTable(){ varaccount=document.getElementById("account").value; varname=document.getElementById("name").value; varpassword=document.getElementById("password").value; row=document.getElementById("table").insertRow(); if(row!=null){ cell=row.insertCell(); cell.innerHTML=account; cell=row.insertCell(); cell.innerHTML=name; cell=row.insertCell(); cell.innerHTML=password; } returnfalse; } </script>可以复制上面的代码做测试。

3、例子所示的功能是当在三个文本框中输入数据后,点击提交按钮,输入的数据会提交到表格中去,而且每提交一次数据,表格会新增加一行数据。效果如图所示:

js如何将表单中数据添加到表格中

4、再次输入可以见到新增一条数据。

js如何将表单中数据添加到表格中

eNSP保存配置文件 mysql怎么创建表 idea怎么设置为中文 HTML中引入css和js的方法 css如何设置背景图居中对齐
热门搜索
图片另存为快捷键 建外soho图片 钛镁合金推拉门图片 好看的电影网站 猛鬼山坟1电影