edit.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/active效果 - 加深颜色,有点击反馈,提升交互体验 */
  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. <!-- 隐藏域保留,无需样式,不影响布局 -->
  103. <input type="hidden" name="id" id="id">
  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="edit()">
  111. </form>
  112. <script>
  113. function edit(){
  114. var id = document.getElementById("id").value
  115. var productName = document.getElementById("productName").value
  116. var title = document.getElementById("title").value
  117. var stocket = document.getElementById("stocket").value
  118. // 新增简单非空校验,避免空数据提交
  119. if(!productName || !stocket || !title){
  120. alert("商品名称、库存、标题不能为空!");
  121. return;
  122. }
  123. var obj = {id:id,productName:productName,title:title,stocket:stocket}
  124. $.post("http://c46a5489.natappfree.cc/product/update",obj,function (result){
  125. if(result.success){
  126. // 重新跳转回product.html界面中
  127. location.href = "/js-demo-day02/js-demo-day02/demo/product.html"
  128. }else{
  129. alert(result.msg || "编辑失败,请重试");
  130. }
  131. },"json") // 指定返回格式为JSON,避免解析异常
  132. .fail(function(){
  133. alert("网络错误,编辑请求发送失败!");
  134. });
  135. }
  136. // 封装获取地址栏参数的通用函数(放在调用前,避免函数未定义报错)
  137. function getUrlParam(name) {
  138. const search = window.location.search.substring(1);
  139. const params = search.split('&');
  140. for (let i = 0; i < params.length; i++) {
  141. const pair = params[i].split('=');
  142. if (pair[0] === name) {
  143. return decodeURIComponent(pair[1] || '');
  144. }
  145. }
  146. return '';
  147. }
  148. // 获取地址栏ID并请求商品详情回显
  149. var id = getUrlParam("id")
  150. // 新增ID非空校验,避免无ID时发送无效请求
  151. if(!id){
  152. alert("未获取到商品ID,无法编辑!");
  153. }else{
  154. console.log("当前编辑商品ID:", id);
  155. $.get("http://c46a5489.natappfree.cc/product/get?id="+id,function(result){
  156. if(result.success){
  157. // 返回result {success: true,msg: 操作成功,data: Product}
  158. var product = result.data
  159. // 把返回数据设置到输入框中
  160. document.getElementById("id").value = product.id
  161. document.getElementById("productName").value = product.productName
  162. document.getElementById("title").value = product.title
  163. document.getElementById("stocket").value = product.stocket
  164. }else{
  165. alert(result.msg || "获取商品详情失败");
  166. }
  167. },"json")
  168. .fail(function(){
  169. alert("网络错误,获取商品详情失败!");
  170. });
  171. }
  172. </script>
  173. </body>
  174. </html>