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

2025-10-03 22:32:58

1、这里用一个例子来说明怎么添加数据。首先新建一个空白的html文件,添加表单数据:三个文本框,一个提交按钮,一个表格。

<body>

<form onsubmit="return submitTable()">

账号:<input type="text" id="account"><br>

姓名:<input type="text" id="name"><br>

密码:<input type="password" id="password"><br>

<input name="submit" type="button" id="submit" value="提交" onclick="return submitTable()">

</form>

<hr>

<table border="1

<tr>

<th>账号</th><th>姓名</th><th>密码</th>

</tr>

</table>

</body>

可以直接复制上面的代码到编辑器中测试。效果如图所示:

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

2、JavaScript的代码:

<script type="text/javascript">

function submitTable(){

var account = document.getElementById("account").value;

var name=document.getElementById("name").value;

var password=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;

}

return false;

}

</script>

可以复制上面的代码做测试。

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

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

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

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

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
相关推荐
  • 阅读量:123
  • 阅读量:80
  • 阅读量:92
  • 阅读量:35
  • 阅读量:153
  • 猜你喜欢