久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

js實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車模塊

瀏覽:136日期:2024-04-01 10:08:13

本文實(shí)例為大家分享了js實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車模塊的具體代碼,供大家參考,具體內(nèi)容如下

js實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車模塊

主要功能 輸入框正則判斷,兩位數(shù)小數(shù),開頭可以為0 如果商品名字相同,自動(dòng)數(shù)量+1,如果名字相同,價(jià)格不同,以最新價(jià)格為準(zhǔn)(有bug,多個(gè)商品無法操作。程序混亂,隨后在改) 選住商品,或添加減少數(shù)量,都會(huì)自動(dòng)更新右下角的價(jià)格和數(shù)量 結(jié)算過的商品自動(dòng)消失 源碼:

1.html

<body> <div align='center'><form> <span class='font1'>名稱:</span><input type='text' id='name'> <span class='font1'>單價(jià):</span><input type='text' id='price'> <input type='button' value='添加'> <input type='button' value='結(jié)算'> <input type='reset' value='重置'></form> </div> <div><table border='1' > <thead> <tr align='center'> <td><input type='checkbox' style=’cursor: pointer’></td> <td>商品名稱</td> <td>價(jià)格</td> <td>數(shù)量</td> <td>操作</td> </tr> </thead> <tbody> </tbody></table> </div> <div id='b'><span>總價(jià):</span><span style='color: red'>0</span>&nbsp;<span>商品數(shù)量:</span><span style='color: red'>0</span> </div></body>

2.css

<style>body{ background-color: coral;}#head{ margin:30px auto 10px auto;}#name,#price{ background-color: aquamarine;}#add1,#pay1,#set1{ color: red; font-weight: bold; background-color: gold; cursor: pointer;}.font1{ font-weight: bold; font-size: large;}#t,#b{ border-collapse: collapse; margin: 30px auto; width: 600px;}#t thead{ border: 3px solid gold; color: white; background-color: blue;}#t tbody{ color: #1414bf; background-color: white;}</style>

js部分

<script src='http://www.baoyu77737.com/lib/jquery-3.3.1.js'></script> <script> //初始化按鈕 function initButton(){ $('input[name=j1]').off(); $('input[name=x1]').off(); //添加數(shù)量按鈕 $('input[name=j1]').on('click', function (){ var num = parseInt($(this).prev().val());if (num > 1){ $(this).prev().prev().attr('disabled',false);}if (num > 9){ $(this).attr('disabled','disabled'); return;} num++; if (num > 1){ $(this).prev().prev().attr('disabled',false); } if (num > 9){ $(this).attr('disabled','disabled'); } $(this).prev().val(num);$('#Total').text(cal());$('#TotalNum').text(calNum()); } ) //減少數(shù)量按鈕 $($('input[name=x1]')).click(function (){var num = parseInt($(this).next().val());if (num-1 < 10){ $('#add1').prop('disabled',false);}num--;if (num < 10){ $(this).next().next().prop('disabled',false);}if (num == 1){ $(this).prop('disabled','disabled');}$(this).next().val(num);$('#Total').text(cal());$('#TotalNum').text(calNum()); }); }//初始化刪除 function initdelete(){ $('.delete').on('click',function (){$(this).parent().parent().remove();$('#Total').text(cal());$('#TotalNum').text(calNum()); }); }//全選或全不選功能 $('thead input[type=checkbox]').on('click',function (){ $('tbody input[type=checkbox]').each(function (index,element){ $(this).prop('checked',$('thead input[type=checkbox]').prop('checked')); $('#Total').text(cal()); $('#TotalNum').text(calNum()); }); }) //初始化每個(gè)選框選中的方法 function initCheckBox(){ $('tbody input[type=checkbox]').off(); $('tbody input[type=checkbox]').on('change',function (){$('#Total').text(cal());$('#TotalNum').text(calNum()); }); } //計(jì)算總價(jià) function cal(){ var price = null; $('tbody input[type=checkbox]:checked').each(function (){var priceByOne = parseFloat($(this).parent().next().next().text());var num = parseFloat($(this).parent().next().next().next().find('input[name=’num’]').val());var totalMoneyByone = priceByOne * num;price+= totalMoneyByone ; }); return price; } //計(jì)算總的數(shù)量 function calNum(){ var totalNum = null; $('tbody input[type=checkbox]:checked').each(function (){var num = parseInt($(this).parent().next().next().next().find('input[name=’num’]').val());totalNum+=num; }); return totalNum; } //結(jié)算 $('#pay1').on('click',function (){ alert('一共消費(fèi):'+cal()); $('thead input[type=checkbox]:checked').prop('checked',false); $('tbody input[type=checkbox]:checked').parent().parent().remove(); }); //添加 $('#add1').on('click',function (){ var name = $('#name').val(); var price = $('#price').val(); var priceZ = /(^[1-9]d*(.d{1,2})?$)|(^0(.d{1,2})?$)/ if ((name == '' || price == '') ||(!priceZ.test(price)) ){alert('輸入錯(cuò)誤!'); }else {var GameArr = [];var flag = false;var repeat = null;//得到名字?jǐn)?shù)組$('tbody').each(function (){ var finds = $(this).find('.goodsName'); for (let i = 0; i < finds.length; i++) { GameArr.push(finds.eq(i).text()); }});for (let i = 0; i < GameArr.length; i++) { if (name == GameArr[i]){ repeat = i; flag = true; break; }}//如果有相同名字,改數(shù)量和價(jià)格if (flag == true){ var totalNum = parseInt($('tbody:eq(' + repeat + ')').find('input[name=’num’]').val())+1; if (totalNum > 9){ $(this).attr('disabled','disabled'); } $('tbody:eq(' + repeat + ')').find('input[name=’num’]').val(totalNum); $('tbody:eq(' + repeat + ')').find('.goodsPrice').text(price); //否則拼接表格}else {var goods = '<tr>'+ '<td><input type=’checkbox’ style=’cursor: pointer’></td>'+ '<td class=’goodsName’>'+name+'</td>'+ '<td class=’goodsPrice’>'+price+'</td>'+ '<td>'+ '<input type=’button’ value=’-’ name=’x1’ style=’cursor: pointer’>&nbsp;'+ '<input type=’text’ value=’1’ name=’num’>&nbsp;'+ '<input type=’button’ value=’+’ name=’j1’ style=’cursor: pointer’>' +'</td>'+ ’<td><a href='http://www.baoyu77737.com/bcjs/14106.html' class=' rel='external nofollow' delete' style='color:red'>刪除</a></td>’ + '</tr>'$('tbody').append(goods);//每次添加完,綁定事件initButton();initdelete();initCheckBox(); }} });</script>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 庆云县| 内乡县| 宣威市| 秭归县| 福州市| 桃源县| 辰溪县| 杨浦区| 含山县| 民县| 长兴县| 丁青县| 博客| 永川市| 武穴市| 宜都市| 唐河县| 定兴县| 益阳市| 临沭县| 安西县| 延安市| 渝中区| 中宁县| 武隆县| 闽清县| 靖江市| 甘南县| 柳林县| 邢台市| 芷江| 廊坊市| 六枝特区| 正宁县| 正阳县| 拜泉县| 迁安市| 商河县| 永新县| 北宁市| 凉山|