本文實例講述了jQuery實現購物車表單自動結算效果。分享給大家供大家參考。具體如下:
這裡jQuery實現購物車表單自動結算,只要用戶把所購商品的數量輸入進去,就可以適時計算出商品總額,金額運費,類似淘寶的購物車結算功能,計算過程是適時的,用jquery實現了Ajax不刷新網頁就計算的功能,做購物類網站的或許可以用上這個例子。
運作效果截圖如下:
具體程式碼如下:
<!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 id="jQuery购物车自动计算表单金额">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的最新趨勢包括TypeScript的崛起、現代框架和庫的流行以及WebAssembly的應用。未來前景涵蓋更強大的類型系統、服務器端JavaScript的發展、人工智能和機器學習的擴展以及物聯網和邊緣計算的潛力。

JavaScript是現代Web開發的基石,它的主要功能包括事件驅動編程、動態內容生成和異步編程。 1)事件驅動編程允許網頁根據用戶操作動態變化。 2)動態內容生成使得頁面內容可以根據條件調整。 3)異步編程確保用戶界面不被阻塞。 JavaScript廣泛應用於網頁交互、單頁面應用和服務器端開發,極大地提升了用戶體驗和跨平台開發的靈活性。

Python更适合数据科学和机器学习,JavaScript更适合前端和全栈开发。1.Python以简洁语法和丰富库生态著称,适用于数据分析和Web开发。2.JavaScript是前端开发核心,Node.js支持服务器端编程,适用于全栈开发。

JavaScript不需要安裝,因為它已內置於現代瀏覽器中。你只需文本編輯器和瀏覽器即可開始使用。 1)在瀏覽器環境中,通過標籤嵌入HTML文件中運行。 2)在Node.js環境中,下載並安裝Node.js後,通過命令行運行JavaScript文件。

如何在Quartz中提前發送任務通知在使用Quartz定時器進行任務調度時,任務的執行時間是由cron表達式設定的。現�...

在JavaScript中如何獲取原型鏈上函數的參數在JavaScript編程中,理解和操作原型鏈上的函數參數是常見且重要的任�...

在微信小程序web-view中使用Vue.js動態style位移失效的原因分析在使用Vue.js...

在Tampermonkey中如何對多個鏈接進行並發GET請求並依次判斷返回結果?在Tampermonkey腳本中,我們經常需要對多個鏈...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

記事本++7.3.1
好用且免費的程式碼編輯器

Dreamweaver CS6
視覺化網頁開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SublimeText3漢化版
中文版,非常好用