add.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>新增商品</title>
  6. <script src="../js/jquery-2.1.4.js"></script>
  7. <style>
  8. /* 全局样式重置 - 统一跨浏览器样式,消除默认边距 */
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. font-family: "Microsoft Yahei", sans-serif;
  14. }
  15. /* 页面背景 - 浅灰背景提升层次感,flex让表单垂直+水平居中 */
  16. body {
  17. background-color: #f5f5f5;
  18. min-height: 100vh;
  19. display: flex;
  20. justify-content: center;
  21. align-items: center;
  22. padding: 20px;
  23. }
  24. /* 表单容器 - 白色卡片、圆角、阴影,和之前页面风格统一 */
  25. form {
  26. background-color: #ffffff;
  27. padding: 40px 35px;
  28. border-radius: 8px;
  29. box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  30. width: 100%;
  31. max-width: 450px;
  32. }
  33. /* 表单标题 - 居中显示,和容器匹配,增加底部间距 */
  34. form h2 {
  35. text-align: center;
  36. color: #333333;
  37. font-size: 24px;
  38. font-weight: 600;
  39. margin-bottom: 30px;
  40. }
  41. /* 标签样式 - 固定宽度,右对齐,统一文字颜色和大小 */
  42. form label {
  43. display: inline-block;
  44. width: 90px;
  45. text-align: right;
  46. color: #666666;
  47. font-size: 16px;
  48. margin-right: 15px;
  49. }
  50. /* 输入框统一样式 - 文本框/数字框共用,美化边框和内边距 */
  51. input[type="text"],
  52. input[type="number"] {
  53. width: calc(100% - 110px);
  54. padding: 12px 15px;
  55. margin: 8px 0 18px 0;
  56. border: 1px solid #e5e6eb;
  57. border-radius: 4px;
  58. font-size: 16px;
  59. color: #333;
  60. transition: all 0.3s ease;
  61. outline: none;
  62. }
  63. /* 输入框聚焦效果 - 蓝色边框+轻阴影,提升交互反馈 */
  64. input[type="text"]:focus,
  65. input[type="number"]:focus {
  66. border-color: #409eff;
  67. box-shadow: 0 0 6px rgba(64, 158, 255, 0.2);
  68. }
  69. /* 数字框去除默认上下箭头 - 美化外观,不影响功能 */
  70. input[type="number"]::-webkit-inner-spin-button,
  71. input[type="number"]::-webkit-outer-spin-button {
  72. -webkit-appearance: none;
  73. margin: 0;
  74. }
  75. /* 保存按钮样式 - 通栏显示,美化配色和交互 */
  76. input[type="button"] {
  77. width: 100%;
  78. padding: 12px 0;
  79. border: none;
  80. border-radius: 4px;
  81. background-color: #409eff;
  82. color: #ffffff;
  83. font-size: 16px;
  84. font-weight: 500;
  85. cursor: pointer;
  86. transition: background-color 0.3s ease;
  87. margin-top: 10px;
  88. }
  89. /* 按钮hover/点击效果 - 加深颜色,提升交互体验 */
  90. input[type="button"]:hover {
  91. background-color: #337ecc;
  92. }
  93. input[type="button"]:active {
  94. background-color: #266fc8;
  95. }
  96. </style>
  97. </head>
  98. <body>
  99. <form action="#">
  100. <!-- 新增表单标题,和整体风格匹配 -->
  101. <h2>新增商品</h2>
  102. <label for="id">商品ID:</label>
  103. <input type="text" name="id" id="id"><br>
  104. <label for="productName">商品名称:</label>
  105. <input type="text" name="productName" id="productName"><br>
  106. <label for="stocket">商品库存:</label>
  107. <input type="number" name="stocket" id="stocket"><br>
  108. <label for="title">商品标题:</label>
  109. <input type="text" name="title" id="title"><br>
  110. <input type="button" value="保存" onclick="add()">
  111. </form>
  112. <script>
  113. function add(){
  114. var id = document.getElementById("id").value
  115. var productName = document.getElementById("productName").value
  116. var stocket = document.getElementById("stocket").value
  117. var title = document.getElementById("title").value
  118. // 简单非空校验 - 新增优化,避免空参数提交
  119. if(!id || !productName || !stocket || !title){
  120. alert("请填写完整的商品信息!");
  121. return;
  122. }
  123. $.post("http://c46a5489.natappfree.cc/product/add",{
  124. id:id,
  125. productName:productName,
  126. title:title,
  127. stocket:stocket
  128. },function(result){
  129. if(result.success){
  130. location.href = "/js-demo-day02/js-demo-day02/demo/product.html"
  131. }else{
  132. alert("添加失败:" + (result.msg || "未知错误"));
  133. }
  134. },"json") // 新增指定返回格式为json,避免解析错误
  135. .fail(function(){ // 新增请求失败回调,提示网络错误
  136. alert("网络错误,添加请求发送失败!");
  137. });
  138. }
  139. </script>
  140. </body>
  141. </html>