$(document).ready(function() { $("#productSearch").on("input", function() { var str = $(this).val(); $.ajax({ type: "GET", url: "Searchproduct.php", dataType: "html", contentType: "application/json;charset=utf-8", data: { "str": str }, timeout: 20000, success: function(e) { $("#productlist").show(); $("#productlist ul").html(e); } }); }); $(".productlist li").live("click", function() { var id = $(this).data("id"); var unit = $(this).data("unit"); var productName = $(this).find(".name").html(); // 检查该产品是否已存在 var existingProduct = false; $("input[name='productId[]']").each(function() { if ($(this).val() == id) { existingProduct = true; return false; // 跳出循环 } }); if (existingProduct) { // 如果产品已存在,显示提示并阻止添加 // 从HTML内容中提取纯文本产品名称(不包含分类标签) var tempDiv = document.createElement("div"); tempDiv.innerHTML = productName; var plainProductName = tempDiv.textContent || tempDiv.innerText || ""; plainProductName = plainProductName.split('[')[0].trim(); // 移除分类部分 alert("产品「" + plainProductName + "」已添加,不能重复添加"); $("#productlist").hide(); $("#productSearch").val(""); return false; } var n = "
" + productName + "
"; var pic = "
" + $(this).find(".pic").html() + "
"; var item = "
" + n + pic + "
"; $(".prowapper").append(item); $("#productlist").hide(); $("#productSearch").val(""); }) $(".prodelet").live("click", function() { $(this).parent().remove(); }) $(".priceitem .additem").live("click", function() { var priceitem = $(this).parent().clone(); var i = $(this).parent().index(); var lastnum = $(".priceitem").eq(i).find(".num").val(); var lastprice = $(".priceitem").eq(i).find(".price").val(); var productId = $(this).parent().find("input[name='productId[]']").val(); if (lastnum == "" || lastprice == "") //未输入无法继续添加 { return false } else { // Make sure cloned item has array notation for field names priceitem.find("input[name='productId[]']").val(productId); priceitem.find(".num").val(""); priceitem.find(".price").val(""); $(this).parent().after(priceitem); } }) $(".priceitem .delitem").live("click", function() { var n = $(this).parent().siblings().length; if (n > 0) { priceitem = $(this).parent().remove(); } else { return false } }) $(".priceitem .price").live("blur", function() { var pprice; //Pre数量 var cprice = $(this).val(); //当前数量 // 找到当前价格所在的产品区域 var currentProduct = $(this).closest('.proprice'); // 在当前产品内找出所有价格项 var priceItems = currentProduct.find('.priceitem'); // 获取当前价格项在当前产品内的索引 var currentIndex = $(this).closest('.priceitem').index(); // 只有当不是第一个价格项时才需要验证 if (currentIndex > 0) { // 获取同一产品内的上一个价格项的价格 pprice = priceItems.eq(currentIndex - 1).find(".price").val(); // 使用parseFloat进行数值比较而不是eval if (pprice && parseFloat(cprice) > parseFloat(pprice)) { $(this).parent().find(".note").html("当前售价不能高于上一项"); $(this).select(); } else { $(this).parent().find(".note").html(""); } } }) $(".priceitem .num").live("blur", function() { var pnum; //Pre数量 var cnum = $(this).val(); //当前数量 // 找到当前数量所在的产品区域 var currentProduct = $(this).closest('.proprice'); // 在当前产品内找出所有价格项 var priceItems = currentProduct.find('.priceitem'); // 获取当前价格项在当前产品内的索引 var currentIndex = $(this).closest('.priceitem').index(); // 只有当不是第一个价格项时才需要验证 if (currentIndex > 0) { // 获取同一产品内的上一个价格项的数量 pnum = priceItems.eq(currentIndex - 1).find(".num").val(); // 使用parseFloat进行数值比较而不是eval if (pnum && parseFloat(cnum) < parseFloat(pnum)) { $(this).parent().find(".note").html("当前数量不能小于上一项"); $(this).select(); } else { $(this).parent().find(".note").html(""); } } }) });