zoukankan      html  css  js  c++  java
  • 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> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.1.min.js"></script> 
    <script> 
    $(function(){ 
    $(".add").click(function(){ 
    var t=$(this).parent().find('input[class*=text_box]'); 
    t.val(parseInt(t.val())+1) 
    setTotal(); 
    }) 
    $(".min").click(function(){ 
    var t=$(this).parent().find('input[class*=text_box]'); 
    t.val(parseInt(t.val())-1) 
    if(parseInt(t.val())<0){ 
    t.val(0); 
    } 
    setTotal(); 
    }) 
    function setTotal(){ 
    var s=0; 
    $("#tab td").each(function(){ 
    s+=parseInt($(this).find('input[class*=text_box]').val())*parseFloat($(this).find('span[class*=price]').text()); 
    }); 
    $("#total").html(s.toFixed(2)); 
    } 
    setTotal(); 
    
    }) 
    </script> 
    </head> 
    <body> 
    <table id="tab"> 
    <tr> 
    <td> 
    <span>单价:</span><span class="price">1.50</span> 
    <input class="min" name="" type="button" value="-" /> 
    <input class="text_box" name="" type="text" value="1" /> 
    <input class="add" name="" type="button" value="+" /> 
    </td> 
    </tr> 
    <tr> 
    <td> 
    <span>单价:</span><span class="price">3.95</span> 
    <input class="min" name="" type="button" value="-" /> 
    <input class="text_box" name="" type="text" value="1" /> 
    <input class="add" name="" type="button" value="+" /> 
    </td> 
    </tr> 
    </table> 
    
    <p>总价:<label id="total"></label></p> 
    </body> 
    </html> 
  • 相关阅读:
    Android 上传图片到服务器 okhttp一
    Kotlin 扩展——省略findViewById
    音频的播放一
    layui+ztree 树状下拉框
    Element里el-badge在el-tab里视图不被渲染问题
    linux之cat 操作
    cmd命令行中查看、修改、删除与添加环境变量
    cmd 文件/文件夹的一切操作
    操作
    11. 判断是给属性前加typeof 可以同时判断属性是否存在
  • 原文地址:https://www.cnblogs.com/xtmp/p/5772172.html
Copyright © 2011-2022 走看看