表单添加表格数据

本文介绍如何利用JavaScript在网页中动态生成表格,并将表单数据填充到表格中,适用于前端开发人员实现动态展示数据的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 <!DOCTYPE html>
 <html>
  
 <head>
 <meta charset="utf-8" />
 <title></title>
 <script src="js/jquery-1.11.0.min.js"></script>
 <script>
 $(function() {
 // $(".btn_del").on('click', function() {
 // alert("123");
 // })
 $(document).on('click', '.btn_del', function() {
 $(this).parent().parent().remove()
 var tr = $("tr");
 if(tr.length == 1) {
 $(".table").hide()
 }
 })
  
 })
 </script>
 <style>
 .table {
 width: 700px;
 }
  
 .table tr th {
 text-align: center;
 color: red;
 background-color: yellow;
 }
  
 .table td {
 border: 1px solid black;
 text-align: center;
 width: 140px;
 }
 </style>
 </head>
  
 <body>
 <hr /> 用户
 <input type="text" class="user" id="user" />密码<input type="password" class="pwd" id="pwd" /> 邮箱
 <input type="text" class="eml" id="eml" /><input type="button" id="btn" value="添加" />
 <input type="button" value="批量删除" id="ip_del" />
 <hr />
 <table class="table" cellpadding="0px" cellspacing="0px">
 <tr id="tr">
 <th><input type="checkbox" id="cb_one" class="cb_one" /></th>
 <th>用户名</th>
 <th>密码</th>
 <th>邮箱</th>
 <th>操作</th>
 </tr>
 </table>
 <script>
 $(".table").hide();
 $("#btn").click(function() {
 $(".table").show();
 var user = $("#user").val();
 var pwd = $("#pwd").val();
 var eml = $("#eml").val();
 var $tr = $("<tr><td><input type='checkbox' class='cb_ins'/></td><td>" + user + "</td><td>" + pwd + "</td><td>" + eml + "</td><td><button class='btn_del'>删除</button></td></tr>")
 $(".table").append($tr);
 /*$(".btn_del").on('click', function() {
 $(this).parent().parent().remove()
 })*/
 })
 var flag = true;
 $("#cb_one").click(function() {
 var aa = $("input[type=checkbox]")
 for(i = 0; i < aa.length; i++) {
 aa[i].checked = flag;
 }
 flag = !flag;
 })
 $("#ip_del").click(function() {
 var $tr = $("input[type=checkbox]:checked");
 if($tr.length == 0) {
 alert("至少选一行")
 }
 for(i = 0; i < $tr.length; i++) {
 if(i == 0) {
 var td = $($tr[i])
 var aa = td.parent().parent();
 if(aa.attr("id") != "tr") {
 alert(aa)
 td.parent().parent().remove();
 }
 } else {
 $tr[i].parentNode.parentNode.remove();
 }
  
 }
 var $tr = $("input[type=checkbox]");
 if($tr.length == 1) {
 $(".table").hide();
 }
 })
 </script>
 </body>
  
 </html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值