jQuery实现购物车表单自动结算效果实例


Posted in Javascript onAugust 10, 2015

本文实例讲述了jQuery实现购物车表单自动结算效果。分享给大家供大家参考。具体如下:

这里jQuery实现购物车表单自动结算,只要用户把所购商品的数量输入进去,就可以适时计算出商品总额,金额+运费,类似淘宝的购物车结算功能,计算过程是适时的,用jquery实现了Ajax不刷新网页就计算的功能,做购物类网站的或许可以用上这个例子。

运行效果截图如下:

jQuery实现购物车表单自动结算效果实例

具体代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery购物车表单自动结算</title>
<style>
*{margin:0;padding:0;}
body{font:12px "Lucida Grande", Helvetica, Sans-Serif;padding:50px;}
table{border-collapse:collapse;}
#order-table{width:100%;}
#order-table td{padding:5px;}
#order-table th{padding:5px;background:black;color:white;text-align:left;}
#order-table td.row-total{text-align:right;}
#order-table td input{width:75px;text-align:center;}
#order-table tr.even td{background:#eee;}
#order-table td .total-box,.total-box{border:3px solid green;width:70px;padding:3px;margin:5px 0 5px 0;text-align:center;font-size:14px;}
#shipping-subtotal{margin:0;}
#shipping-table{width:350px;float:right;}
#shipping-table td{padding:5px;}
#shipping-table th{padding:5px;background:black;color:white;text-align:left;}
#shipping-table td input{width:69px;text-align:center;}
#order-total{font-weight:bold;font-size:21px;width:110px;}
</style>
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script type="text/javascript">
function IsNumeric(sText)
{
 var ValidChars = "0123456789.";
 var IsNumber=true;
 var Char;
 for (i = 0; i < sText.length && IsNumber == true; i++)
 {
 Char = sText.charAt(i);
 if (ValidChars.indexOf(Char) == -1)
  {
  IsNumber = false;
  }
 }
 return IsNumber;
};
function calcProdSubTotal() {
 var prodSubTotal = 0;
 $(".row-total-input").each(function(){
 var valString = $(this).val() || 0;
 prodSubTotal += parseInt(valString);
 });
 $("#product-subtotal").val(prodSubTotal);
};
function calcTotalPallets() {
 var totalPallets = 0;
 $(".num-pallets-input").each(function() {
 var thisValue = $(this).val();
 if ( (IsNumeric(thisValue)) && (thisValue != '') ) {
  totalPallets += parseInt(thisValue);
 };
 });
 $("#total-pallets-input").val(totalPallets);
};
function calcShippingTotal() {
 var totalPallets = $("#total-pallets-input").val() || 0;
 var shippingRate = $("#shipping-rate").text() || 0;
 var shippingTotal = totalPallets * shippingRate;
 $("#shipping-subtotal").val(shippingTotal);
};
function calcOrderTotal() {
 var orderTotal = 0;
 var productSubtotal = $("#product-subtotal").val() || 0;
 var shippingSubtotal = $("#shipping-subtotal").val() || 0;
 var orderTotal = parseInt(productSubtotal) + parseInt(shippingSubtotal);
 var orderTotalNice = "$" + orderTotal;
 $("#order-total").val(orderTotalNice);
};
$(function(){
 $('.num-pallets-input').blur(function(){
 var $this = $(this);
 var numPallets = $this.val();
 var multiplier = $this
    .parent().parent()
    .find("td.price-per-pallet span")
    .text();
 if ( (IsNumeric(numPallets)) && (numPallets != '') ) {
  var rowTotal = numPallets * multiplier;
  $this
  .css("background-color", "white")
  .parent().parent()
  .find("td.row-total input")
  .val(rowTotal);
 } else {
  $this.css("background-color", "#ffdcdc");
 };
 calcProdSubTotal();
 calcTotalPallets();
 calcShippingTotal();
 calcOrderTotal();
 });
});
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
<body>
<h1>jQuery购物车自动计算表单金额</h1>
<table id="order-table">
 <tr>
  <th>商品名称</th>
  <th>数量</th>
  <th>X</th>
  <th>单价</th>
  <th>=</th>
  <th style="text-align: right;">总计</th>
 </tr>
 <tr class="odd">
  <td class="product-title">裤子</td>
  <td class="num-pallets">
   <input type="text" class="num-pallets-input" id="turface-pro-league-num-pallets" ></input>
  </td>
  <td class="times">X</td>
  <td class="price-per-pallet">$<span>340</span></td>
  <td class="equals">=</td>
  <td class="row-total">
   <input type="text" class="row-total-input" id="turface-pro-league-row-total" disabled="disabled"></input>
  </td>
 </tr>
 <tr class="even">
  <td class="product-title">袜子</td>
  <td class="num-pallets">
   <input type="text" class="num-pallets-input" id="turface-pro-league-red-num-pallets"></input>
  </td>
  <td class="times">X</td>
  <td class="price-per-pallet">$<span>455</span></td>
  <td class="equals">=</td>
  <td class="row-total">
   <input type="text" class="row-total-input" id="turface-pro-league-red-row-total" disabled="disabled"></input>
  </td>
 </tr>
 <tr class="odd">
  <td class="product-title">婴儿用品</td>
  <td class="num-pallets">
   <input type="text" class="num-pallets-input" id="turface-quick-dry-num-pallets" ></input>
  </td>
  <td class="times">X</td>
  <td class="price-per-pallet">$<span>300</span></td>
  <td class="equals">=</td>
  <td class="row-total">
   <input type="text" class="row-total-input" id="turface-quick-dry-row-total" disabled="disabled"></input>
  </td>
 </tr>
 <tr class="even">
  <td class="product-title">电脑用品</td>
  <td class="num-pallets">
   <input type="text" class="num-pallets-input" id="turface-mound-clay-red-num-pallets"></input>
  </td>
  <td class="times">X</td>
  <td class="price-per-pallet">$<span>410</span></td>
  <td class="equals">=</td>
  <td class="row-total">
   <input type="text" class="row-total-input" id="turface-mound-clay-red-row-total" disabled="disabled"></input>
  </td>
 </tr>
 <tr class="odd">
  <td class="product-title">汽车装饰用品</td>
  <td class="num-pallets">
   <input type="text" class="num-pallets-input" id="diamond-pro-red-num-pallets" ></input>
  </td>
  <td class="times">X</td>
  <td class="price-per-pallet">$<span>365</span></td>
  <td class="equals">=</td>
  <td class="row-total">
   <input type="text" class="row-total-input" id="diamond-pro-red-row-total" disabled="disabled"></input>
  </td>
 </tr>
 <tr class="even">
  <td class="product-title">家居装饰用品</em></td>
  <td class="num-pallets">
   <input type="text" class="num-pallets-input" id="diamond-pro-drying-agent-num-pallets"></input>
  </td>
  <td class="times">X</td>
  <td class="price-per-pallet">$<span>340</span></td>
  <td class="equals">=</td>
  <td class="row-total">
   <input type="text" class="row-total-input" id="diamond-pro-drying-agent-row-total" disabled="disabled"></input>
  </td>
 </tr>
 <tr class="odd">
  <td class="product-title">生活用品</td>
  <td class="num-pallets">
   <input type="text" class="num-pallets-input" id="diamond-pro-professional-num-pallets" ></input>
  </td>
  <td class="times">X</td>
  <td class="price-per-pallet">$<span>375</span></td>
  <td class="equals">=</td>
  <td class="row-total">
   <input type="text" class="row-total-input" id="diamond-pro-professional-row-total" disabled="disabled"></input>
  </td>
 </tr>
 <tr class="even">
  <td class="product-title">建材用品</td>
  <td class="num-pallets">
   <input type="text" class="num-pallets-input" id="diamond-pro-top-dressing-num-pallets"></input>
  </td>
  <td class="times">X</td>
  <td class="price-per-pallet">$<span>340</span></td>
  <td class="equals">=</td>
  <td class="row-total">
   <input type="text" class="row-total-input" id="diamond-pro-top-dressing-row-total" disabled="disabled"></input>
  </td>
 </tr>
 <tr>
  <td colspan="6" style="text-align: right;">产品小计:
   <input type="text" class="total-box" id="product-subtotal" disabled="disabled"></input>
  </td>
 </tr>
</table>
<table id="shipping-table">
 <tr>
  <th>总数量.</th>
  <th>X</th>
  <th>运费</th>
  <th>=</th>
  <th style="text-align: right;">总运费</th>
 </tr>
 <tr>
  <td id="total-pallets">
   <input id="total-pallets-input" type="text" disabled="disabled"></input>
  </td>
  <td>X</td>
  <td id="shipping-rate">10.00</td>
  <td>=</td>
  <td style="text-align: right;">
  <input type="text" class="total-box" id="shipping-subtotal" disabled="disabled"></input>
  </td>
 </tr>
</table>
<div class="clear"></div>
<div style="text-align:right;">
 <span>订单总额: </span>
 <input type="text" class="total-box" id="order-total" disabled="disabled"></input>
 <br /><br />
 <input type="submit" value="提交结账" class="submit" />
</div>
</body>
</html>

希望本文所述对大家的jquery程序设计有所帮助。

Javascript 相关文章推荐
JavaScript 调试器简介
Feb 21 Javascript
让你的博文自动带上缩址的实现代码,方便发到微博客上
Dec 28 Javascript
js获取客户端网卡的IP地址、MAC地址
Mar 26 Javascript
jQuery轻松实现表格的隔行变色和点击行变色的实例代码
May 09 Javascript
基于jQuery实现的幻灯图片切换
Dec 02 Javascript
vue内置组件transition简单原理图文详解(小结)
Jul 12 Javascript
深入理解Vue 的钩子函数
Sep 05 Javascript
解决layer图标icon不加载的问题
Sep 04 Javascript
JS数组扁平化、去重、排序操作实例详解
Feb 24 Javascript
JS绘图Flot应用图形绘制异常解决方案
Oct 16 Javascript
用javascript实现倒计时效果
Feb 09 Javascript
用几道面试题来看JavaScript执行机制
Apr 30 Javascript
javascript中$(function() {});写与不写有哪些区别
Aug 10 #Javascript
jQuery中$(function() {});问题详解
Aug 10 #Javascript
jquery实现鼠标滑过后动态图片提示效果实例
Aug 10 #Javascript
jQuery模拟原生态App上拉刷新下拉加载更多页面及原理
Aug 10 #Javascript
jQuery 判断图片是否加载完成方法汇总
Aug 10 #Javascript
javascript实现连续赋值
Aug 10 #Javascript
jQuery+css3实现Ajax点击后动态删除功能的方法
Aug 10 #Javascript
You might like
《APMServ 5.1.2》使用图解
2006/10/23 PHP
可以在线执行PHP代码包装修正版
2008/03/15 PHP
PHP测试框架PHPUnit组织测试操作示例
2018/05/28 PHP
基于JQuery的抓取博客园首页RSS的代码
2011/12/01 Javascript
select标签模拟/美化方法采用JS外挂式插件
2013/04/01 Javascript
JS 实现导航栏悬停效果(续)
2013/09/24 Javascript
对JavaScript中this指针的新理解分享
2015/01/31 Javascript
javascript 判断两个日期之差的示例代码
2015/09/05 Javascript
jquery实现仿新浪微博带动画效果弹出层代码(可关闭、可拖动)
2015/10/12 Javascript
jquery多级树形下拉菜单的实例代码
2019/07/09 jQuery
关于JS解构的5种有趣用法
2019/09/05 Javascript
JS Generator 函数的含义与用法实例总结
2020/04/08 Javascript
浅谈React中组件逻辑复用的那些事儿
2020/05/21 Javascript
使用 UniApp 实现小程序的微信登录功能
2020/06/09 Javascript
python标准算法实现数组全排列的方法
2015/03/17 Python
简单谈谈python中的语句和语法
2017/08/10 Python
Python编程pygame模块实现移动的小车示例代码
2018/01/03 Python
浅析Python装饰器以及装饰器模式
2018/05/28 Python
python查看列的唯一值方法
2018/07/17 Python
python3正则提取字符串里的中文实例
2019/01/31 Python
Python从文件中读取数据的方法讲解
2019/02/14 Python
python pip安装包出现:Failed building wheel for xxx错误的解决
2019/12/25 Python
python PIL模块的基本使用
2020/09/29 Python
用Python自动清理系统垃圾的实现
2021/01/18 Python
详解CSS3的opacity属性设置透明效果的用法
2016/05/09 HTML / CSS
您附近的水疗和健康场所:Spafinder(美国)
2019/07/05 全球购物
销售副总经理岗位职责
2013/12/11 职场文书
一份教室追逐打闹的检讨书
2014/09/27 职场文书
2014小学教师个人工作总结
2014/11/10 职场文书
2015社区爱国卫生工作总结
2015/04/21 职场文书
2015年迎新晚会策划书
2015/07/16 职场文书
化工生产实习心得体会
2016/01/22 职场文书
全家福照片寄语怎么写?
2019/04/02 职场文书
用JS实现飞机大战小游戏
2021/06/09 Javascript
Python卷积神经网络图片分类框架详解分析
2021/11/07 Python
如何用H5实现好玩的2048小游戏
2022/07/23 HTML / CSS