以上是我的购物车。下面我显示流程。
[产品图像点击] - > AJAX请求获取通过jQuery从数据库 细节对于该特定产品ID和附加(产品名称,ID和价格 自动附加)。
[QTY的onfocus] - >的jquery autocalculates子总和显示 - >
还发送AJAX请求计算总,GST和总支付和 这就是问题的开始位置。
由于Ajax请求作出的onfocus,每次数量文本框是 重点,它关系到PHP页面,并为总,GST 和总付款的计算量。因此不断增加超越逻辑。
我保存传递给会话中的php页面的所有值。所以每次,ajax请求都会携带qty和它总是添加到会话中的子总值。为了节省麻烦,我想尝试纯白色区域的客户端计算,就像我如何计算子总数而不提出Ajax请求。
有什么方法,有人可以给我想法如何为所有项目添加总数量和总计sub_total,而无需发送到PHP页面进行计算?也许可以向我介绍jQuery数组排序的事情。我尝试在网上搜索,但无法理解。
HTML
<form action="#" method="POST" id="cart_form">
<table class='header_tbl' style="background:none !important;">
<thead>
<tr>
<th>Product</th>
<th>Price</th>
<th>Qty</th>
<th>Sub Total</th>
<th></th>
</tr>
</thead>
<tbody>
</tbody>
<tfoot>
<tr>
<td>Total</td><td></td><td><span class='qty_1'></span><input type='hidden' name='total_qty' value=''></td><td><span class='total'></span><input type='hidden' name='total' value=''></td><td></td>
</tr>
<tr>
<td>GST 6%</td><td></td><td></td><td><span class='gst'></span><input type='hidden' name='gst' value=''></td><td></td>
</tr>
<tr>
<td>Discount (x%)</td><td></td><td></td><td></td><td></td>
</tr>
<tr>
<td>Total Payment</td><td></td><td></td><td><span class='grand'></span><input type='hidden' name='grand' value=''></td><td></td>
</tr>
<tr><td colspan="5" class="checkout"><input type="submit" name='submit' id="checkout" value="CHECK OUT"/></td></tr>
</tfoot>
</table>
</form>
脚本添加元素融入形式
for (i = 0; i < data.length; i++) {
$(".header_tbl tbody").prepend("<tr id='"+data[i].id+"'><th class='product'>"+data[i].catalog_name+"<input type='hidden' name='catalog_name[]' value='"+data[i].catalog_name+"'></th><td class='price'>RM <span>"+data[i].price_normal+"</span><input type='hidden' name='"+data[i].id+"_price[]' value='"+data[i].price_normal+"'></td><td class='qty_"+data[i].id+"'><input type='text' name='qty' style='width:50px;height:20px;margin:10px auto;' onfocus='subTotal(\""+data[i].id+"\")' value=''><input type='hidden' name='"+data[i].id+"_qty2[]' value=''></td><td class='sub_total'><span class='sub_total_"+data[i].id+"'></span><input type='hidden' name='"+data[i].id+"_sub_total[]' value=''></td><td><img src='"+p_img_path+"del.png' style='width:15px;cursor:pointer;' onClick='del_this(\""+data[i].id+"\")'></td></tr>");
}
您是否尝试将onchange事件附加到您的量子盒来进行计算,然后事件被解雇? – Bindrid
由于每个产品的数量和价格都出现在DOM本身内部的客户端,您可以使用jquery在客户端自己计算。在这里发布您的DOM结构 – dreamweiver
@dreamweiver,我在我的文章中加入了我的html和脚本 – 112233