经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » jQuery » 查看文章
编辑表格输入内容、根据input输入框输入数字动态生成表格行数、编辑表格内容提交传给后台数据处理
来源:cnblogs  作者:vaelcy  时间:2019/11/8 12:27:00  对本文有异议

编辑表格输入内容、根据input输入框输入数字动态生成表格行数、编辑表格内容提交传给后台数据处理

记录自己学习做的东西,写的小demo,希望对大家也有帮助!

代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. </head>
  7. <link rel="stylesheet" href="../../js/bootstrap-3.3.7-dist/css/bootstrap.min.css" />
  8. <script type="text/javascript" src="../../js/jquery-1.12.1.min.js"></script>
  9. <script type="text/javascript" src="../../js/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
  10. <style type="text/css">
  11. table thead tr th {
  12. border-bottom: 0 !important;
  13. }
  14. .table {
  15. margin-top: 20px;
  16. width: 80%;
  17. margin-left: 20px;
  18. }
  19. table tr,
  20. th,
  21. td {
  22. text-align: center;
  23. }
  24. .tdpadding {
  25. padding: 0 !important;
  26. }
  27. .table_input {
  28. width: 100%;
  29. height: 37px;
  30. border: none;
  31. }
  32. </style>
  33.  
  34. <body>
  35.  
  36. <p id="demo"></p>
  37. <label>楼号:</label><input name="" type="text" class="louhao">
  38. <label>单元数:</label><input type="text" id="myInput" oninput="myFunction()">
  39. <form id="submitForm">
  40. <table class="table table-bordered">
  41. <thead class="aa">
  42. <tr>
  43. <th>单元</th>
  44. <th>开始楼层</th>
  45. <th>结束楼层</th>
  46. <th>每层次数</th>
  47. </tr>
  48. </thead>
  49. <tbody class="units">
  50. </tbody>
  51. </table>
  52. </form>
  53. </body>
  54. <button class="btn">提交</button>
  55. <script>
  56. function myFunction() {
  57. var x = $("#myInput").val();
  58. $("#demo").text("你输入的是: " + x);
  59. $(".units").html("");
  60. var str = ""
  61. for(var i = 0; i < x; i++) {
  62. str += "<tr><td class='tdpadding'><input name='inp0' value='" + (i + 1) + "' type='text' readonly='readonly' class='table_input desa'></td><td class='tdpadding'><input name='inp1' value='' type='text' class='table_input'></td><td class='tdpadding'><input name='inp2' value='' type='text' class='table_input'></td><td class='tdpadding'><input name='inp3' value='' type='text' type='text' class='table_input'></td></tr>"
  63. }
  64. $(".units").append(str)
  65. }
  66. $(".btn").click(function() {
  67. var louhao = $(".louhao").val()
  68. console.log(louhao)
  69. var msg = $("#submitForm").serialize();
  70. var json = "[{";
  71. var msg2 = msg.split("&"); //先以“&”符号进行分割,得到一个key=value形式的数组
  72. var t = false;
  73. for(var i = 0; i < msg2.length; i++) {
  74. var msg3 = msg2[i].split("="); //再以“=”进行分割,得到key,value形式的数组
  75. for(var j = 0; j < msg3.length; j++) {
  76. json += "\"" + msg3[j] + "\"";
  77. if(j + 1 != msg3.length) {
  78. json += ":";
  79. }
  80. if(t) {
  81. json += "}";
  82. if(i + 1 != msg2.length) { //表示是否到了当前行的最后一列
  83. json += ",{";
  84. }
  85. t = false;
  86. }
  87. if(msg3[j] == "inp3") { //这里的“inp3”是你的表格的最后一列的input标签的name值,表示是否到了当前行的最后一个input
  88. t = true;
  89. }
  90. }
  91. if(!msg2[i].match("inp3")) { //同上
  92. json += ";";
  93. }
  94. }
  95. json += "]";
  96. console.log(json)
  97. //最终msg的值就被转换为:[{"key":"value";"key":"value"},{"key":"value";"key":"value"}]格式的json数据<br>
  98. })
  99. </script>
  100. </html>

 

原文链接:http://www.cnblogs.com/vaelcy/p/11819086.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号