用JS模擬購物車(jQuery實現)

jQueryShoppingCart.html
jQueryCart.html
<!DOCTYPE html>
<html>
  <head>
    <title>購物車</title>
    <meta charset="utf-8" />
    <style type="text/css">
      h1 {
        text-align:center;
      }
      table {
        margin:0 auto;
        width:60%;
        border:2px solid #aaa;
        border-collapse:collapse;
      }
      table th, table td {
        border:2px solid #aaa;
        padding:5px;
      }
      th {
        background-color:#eee;
      }
    </style>
    <script src="js/jquery-1.11.1.js"></script>
    <script>
    	function add_shoppingcart(btn) {
    		var $tr = $(btn).parent().parent();
    		var name = $tr.children().eq(0).html();
    		var price = parseInt($tr.children().eq(1).html());
    		
    		var $ntr = $(
    			'<tr>'+
          '<td>'+name+'</td>'+
          '<td>'+price+'</td>'+
          '<td align="center">'+
            '<input type="button" value="-" οnclick="reduce_amount(this);"/> '+
            '<input type="text" size="3" readonly value="1"/>'+
            '<input type="button" value="+" οnclick="add_amount(this);"/> '+
          '</td>'+
          '<td>'+price+'</td>'+
          '<td align="center"><input type="button" value="x" οnclick="del_shoppingcart(this);"/></td>'+
        '</tr>'
    		);
    		
    		var $tbody = $("#goods");
    		$tbody.append($ntr);
    		
    		changeTotal();
    	}
    	
    	function del_shoppingcart(btn) {
    		var $tr = $(btn).parent().parent();
    		$tr.remove();
    		
    		changeTotal();
    	}
    	
    	function reduce_amount(btn) {
    		var amount = parseInt($(btn).next().val());
    		if(amount==0) {
    			return;
    		}
    		amount--;
    		$(btn).next().val(amount);
    		
    		var price = parseInt($(btn).parent().prev().html());
    		$(btn).parent().next().html(parseInt($(btn).parent().next().html())-price);
    		
    		changeTotal();
    	}
    	
    	function add_amount(btn) {
    		var amount = parseInt($(btn).prev().val());
    		amount++;
    		$(btn).prev().val(amount);
    		
    		var price = parseInt($(btn).parent().prev().html());
    		$(btn).parent().next().html(parseInt($(btn).parent().next().html())+price);
    	
    		changeTotal();
    	}
    	
    	function changeTotal() {
    		var total = 0;
    		var $trs = $("#goods tr");
    		var price = 0;
    		$trs.each(function() {
    			price = parseInt($(this).children().eq(3).html());
    			total += price
    		});
    		$("#total").html(total);
    	}
    </script>
  </head>
  <body>
    <h1>真划算</h1>
    <table>
      <tr>
        <th>商品</th>
        <th>單價(元)</th>
        <th>顏色</th>
        <th>庫存</th>
        <th>好評率</th>
        <th>操作</th>
      </tr>   
      <tr>
        <td>羅技M185鼠標</td>
        <td>80</td>
        <td>黑色</td>
        <td>893</td>
        <td>98%</td>
        <td align="center">
          <input type="button" value="加入購物車" οnclick="add_shoppingcart(this);"/>
        </td>
      </tr>
      <tr>
        <td>微軟X470鍵盤</td>
        <td>150</td>
        <td>黑色</td>
        <td>9028</td>
        <td>96%</td>
        <td align="center">
          <input type="button" value="加入購物車" οnclick="add_shoppingcart(this);"/>
        </td>
      </tr>
      <tr>
        <td>洛克iphone6手機殼</td>
        <td>60</td>
        <td>透明</td>
        <td>672</td>
        <td>99%</td>
        <td align="center">
          <input type="button" value="加入購物車" οnclick="add_shoppingcart(this);"/>
        </td>
      </tr>
      <tr>
        <td>藍牙耳機</td>
        <td>100</td>
        <td>藍色</td>
        <td>8937</td>
        <td>95%</td>
        <td align="center">
          <input type="button" value="加入購物車" οnclick="add_shoppingcart(this);"/>
        </td>
      </tr>
      <tr>
        <td>金士頓U盤</td>
        <td>70</td>
        <td>紅色</td>
        <td>482</td>
        <td>100%</td>
        <td align="center">
          <input type="button" value="加入購物車" οnclick="add_shoppingcart(this);"/>
        </td>
      </tr>
    </table>
  
    <h1>購物車</h1>
    <table>
      <thead>
        <tr>
          <th>商品</th>
          <th>單價(元)</th>
          <th>數量</th>
          <th>金額(元)</th>
          <th>刪除</th>
        </tr>
      </thead>
      <tbody id="goods">
      </tbody>
      <tfoot>
        <tr>
          <td colspan="3" align="right">總計</td>
          <td id="total"></td>
          <td></td>
        </tr>
      </tfoot>
    </table>    
  </body>
</html>

發佈了42 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章