Searchproduct.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. $(document).ready(function() {
  2. $("#productSearch").on("input",
  3. function() {
  4. var str = $(this).val();
  5. $.ajax({
  6. type: "GET",
  7. url: "Searchproduct.php",
  8. dataType: "html",
  9. contentType: "application/json;charset=utf-8",
  10. data: {
  11. "str": str
  12. },
  13. timeout: 20000,
  14. success: function(e) {
  15. $("#productlist").show();
  16. $("#productlist ul").html(e);
  17. }
  18. });
  19. });
  20. $(".productlist li").live("click",
  21. function() {
  22. var id = $(this).data("id");
  23. var unit = $(this).data("unit");
  24. var productName = $(this).find(".name").html();
  25. // 检查该产品是否已存在
  26. var existingProduct = false;
  27. $("input[name='productId[]']").each(function() {
  28. if ($(this).val() == id) {
  29. existingProduct = true;
  30. return false; // 跳出循环
  31. }
  32. });
  33. if (existingProduct) {
  34. // 如果产品已存在,显示提示并阻止添加
  35. // 从HTML内容中提取纯文本产品名称(不包含分类标签)
  36. var tempDiv = document.createElement("div");
  37. tempDiv.innerHTML = productName;
  38. var plainProductName = tempDiv.textContent || tempDiv.innerText || "";
  39. plainProductName = plainProductName.split('[')[0].trim(); // 移除分类部分
  40. alert("产品「" + plainProductName + "」已添加,不能重复添加");
  41. $("#productlist").hide();
  42. $("#productSearch").val("");
  43. return false;
  44. }
  45. var n = "<div class='proname'>" + productName + "</div>";
  46. var pic = "<div class='propic'>" + $(this).find(".pic").html() + "</div>";
  47. var item = "<div class='proitem'><div class='prodelet'></div>" + n + pic + "<div class='proprice'><div class='priceitem'><input type='hidden' name='productId[]' value="+id+"><label>≥</label><input type='number' autocomplete='off' class='txt3 num' name='num[]'><label class='unit'>" + unit + "</label> <label>售价</label><input type='text' class='txt3 price' autocomplete='off' name='price[]'><label>RMB</label> <span class='additem'></span><span class='delitem'></span><span class='note'></span></div></div></div>";
  48. $(".prowapper").append(item);
  49. $("#productlist").hide();
  50. $("#productSearch").val("");
  51. })
  52. $(".prodelet").live("click",
  53. function() {
  54. $(this).parent().remove();
  55. })
  56. $(".priceitem .additem").live("click",
  57. function() {
  58. var priceitem = $(this).parent().clone();
  59. var i = $(this).parent().index();
  60. var lastnum = $(".priceitem").eq(i).find(".num").val();
  61. var lastprice = $(".priceitem").eq(i).find(".price").val();
  62. var productId = $(this).parent().find("input[name='productId[]']").val();
  63. if (lastnum == "" || lastprice == "") //未输入无法继续添加
  64. {
  65. return false
  66. } else {
  67. // Make sure cloned item has array notation for field names
  68. priceitem.find("input[name='productId[]']").val(productId);
  69. priceitem.find(".num").val("");
  70. priceitem.find(".price").val("");
  71. $(this).parent().after(priceitem);
  72. }
  73. })
  74. $(".priceitem .delitem").live("click",
  75. function() {
  76. var n = $(this).parent().siblings().length;
  77. if (n > 0) {
  78. priceitem = $(this).parent().remove();
  79. } else {
  80. return false
  81. }
  82. })
  83. $(".priceitem .price").live("blur",
  84. function() {
  85. var pprice; //Pre数量
  86. var cprice = $(this).val(); //当前数量
  87. // 找到当前价格所在的产品区域
  88. var currentProduct = $(this).closest('.proprice');
  89. // 在当前产品内找出所有价格项
  90. var priceItems = currentProduct.find('.priceitem');
  91. // 获取当前价格项在当前产品内的索引
  92. var currentIndex = $(this).closest('.priceitem').index();
  93. // 只有当不是第一个价格项时才需要验证
  94. if (currentIndex > 0) {
  95. // 获取同一产品内的上一个价格项的价格
  96. pprice = priceItems.eq(currentIndex - 1).find(".price").val();
  97. // 使用parseFloat进行数值比较而不是eval
  98. if (pprice && parseFloat(cprice) > parseFloat(pprice)) {
  99. $(this).parent().find(".note").html("当前售价不能高于上一项");
  100. $(this).select();
  101. }
  102. else {
  103. $(this).parent().find(".note").html("");
  104. }
  105. }
  106. })
  107. $(".priceitem .num").live("blur",
  108. function() {
  109. var pnum; //Pre数量
  110. var cnum = $(this).val(); //当前数量
  111. // 找到当前数量所在的产品区域
  112. var currentProduct = $(this).closest('.proprice');
  113. // 在当前产品内找出所有价格项
  114. var priceItems = currentProduct.find('.priceitem');
  115. // 获取当前价格项在当前产品内的索引
  116. var currentIndex = $(this).closest('.priceitem').index();
  117. // 只有当不是第一个价格项时才需要验证
  118. if (currentIndex > 0) {
  119. // 获取同一产品内的上一个价格项的数量
  120. pnum = priceItems.eq(currentIndex - 1).find(".num").val();
  121. // 使用parseFloat进行数值比较而不是eval
  122. if (pnum && parseFloat(cnum) < parseFloat(pnum)) {
  123. $(this).parent().find(".note").html("当前数量不能小于上一项");
  124. $(this).select();
  125. }
  126. else {
  127. $(this).parent().find(".note").html("");
  128. }
  129. }
  130. })
  131. });