jQuery 动态添加表格

本是古典 何须时尚 2023-10-06 22:14 180阅读 0赞

阅读目录

  • 预览
  • 源码

预览

在这里插入图片描述

源码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  7. <script>
  8. $(function () {
  9. //提交按钮事件
  10. $("#submit").click(function () {
  11. var tr = $("<tr>");
  12. tr.append($("<td>").html($("#name").val()));
  13. tr.append($("<td>").html($("#email").val()));
  14. tr.append($("<td>").html($("#salary").val()));
  15. btn_del = $("<input type=button value='Delete'/> ");
  16. btn_del.click(function () {
  17. $(this).parent().remove();
  18. })
  19. tr.append(btn_del);
  20. $("#table").append(tr);
  21. })
  22. })
  23. </script>
  24. </head>
  25. <body>
  26. <div id="div1" align="center">
  27. <label style="vertical-align: middle">添加新员工</label><br>
  28. name:<input type="text" id="name">
  29. email:<input type="text" id="email">
  30. salary:<input type="text" id="salary"><br>
  31. <input type="button" value="submit" id="submit" align="center">
  32. <hr>
  33. <table border="1" width="800px" id="table">
  34. <tr>
  35. <th>Name</th>
  36. <th>Email</th>
  37. <th>Salary</th>
  38. <th>Option</th>
  39. </tr>
  40. </table>
  41. </div>
  42. </body>
  43. </html>

发表评论

表情:
评论列表 (有 0 条评论,180人围观)

还没有评论,来说两句吧...

相关阅读