请高手帮我看看,这个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 *tdprice").each(function(){
s+=parseInt($(this).find('tr[class*=text_box]').val())*parseFloat($(this).find('tr[class*=price]').text());
});
$("#total").html(s.toFixed(2));
}
setTotal();
})
</script>
</head>
<body>
<form action="form.php" method="post">
<table id="tab" border="1">
<tr class="tdprice">
<td ><span>单价:</span><span class="price">2.0</span></td>
<td>
<input class="min" name="" type="button" value="-" />
<input class="text_box" name="" type="text" value="1" />
<input class="add" name="" type="button" value="+" />
</td>
<td ><span>小计:¥</span></td>
</tr>
<tr class="tdprice">
<td class="tdprice"><span>单价:</span><span class="price">1.95</span></td>
<td>
<input class="min" name="" type="button" value="-" />
<input class="text_box" name="" type="text" value="1" />
<input class="add" name="" type="button" value="+" />
</td>
<td ><span>小计:¥</span></td>
</tr>
<tr>
<td>国籍</td>
<td>
中国
</td>
</tr>
</table>

<p>总价:<label id="total"></label></p>
</form>
</body>
</html>
最新回答
后悔那段情╮

2024-10-17 08:28:31

<!DOCTYPE html>
<html>

<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();
            });
            setTotal();
        })

        function setTotal() {
            var sum = 0;
            $(".price").each(function(i, dom) {
                var me = $(this);
                var tds = me.closest("td").nextAll("td");
                var txt = tds.eq(0).children(":text");
                var t = tds.eq(1).children("span");
                var s = me.text() * txt.val();
                sum += s;
                t.text("小计:¥" + s);
            });
            $("#total").html(sum.toFixed(2));
        }
    </script>
</head>

<body>
    <form action="form.php" method="post">
        <table id="tab" border="1">
            <tr class="tdprice">
                <td><span>单价:</span><span class="price">2.0</span>
                </td>
                <td>
                    <input class="min" name="" type="button" value="-" />
                    <input class="text_box" name="" type="text" value="1" />
                    <input class="add" name="" type="button" value="+" />
                </td>
                <td><span>小计:¥</span>
                </td>
            </tr>
            <tr class="tdprice">
                <td class="tdprice"><span>单价:</span><span class="price">1.95</span>
                </td>
                <td>
                    <input class="min" name="" type="button" value="-" />
                    <input class="text_box" name="" type="text" value="1" />
                    <input class="add" name="" type="button" value="+" />
                </td>
                <td><span>小计:¥</span>
                </td>
            </tr>
            <tr>
                <td>国籍</td>
                <td>中国</td>
            </tr>
        </table>
        <p>总价:
            <label id="total"></label>
        </p>
    </form>
</body>

</html>
快乐很简单

2024-10-17 09:37:58

<!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> 
<meta charset="utf-8">
<script type="text/javascript" src="
http://code.jquery.com/jquery-1.6.1.min.js"></script> 


</head> 
<body> 
<form action="form.php" method="post">
        <table id="tab" border="1"> 
             <tr class="tdprice"> 
              <td ><span>单价:</span><span class="price">2.0</span></td> 
                <td> 
                    <input class="min" name="" type="button" value="-" /> 
                    <input class="text_box" name="" type="text" value="1" /> 
                    <input class="add" name="" type="button" value="+" /> 
                </td> 
                 <td ><span>小计:<span class="total"></span>¥</span></td> 
            </tr> 
            <tr class="tdprice"> 
                <td class="tdprice"><span>单价:</span><span class="price">1.95</span></td> 
                <td> 
                    <input class="min" name="" type="button" value="-" /> 
                    <input class="text_box" name="" type="text" value="1" /> 
                    <input class="add" name="" type="button" value="+" /> 
                </td> 
                <td ><span>小计:<span class="total"></span>¥</span></td> 
            </tr> 
            <tr> 
              <td>国籍</td> 
                <td> 
中国
                </td> 
            </tr> 
        </table> 
        
        <p>总价:<label id="total"></label></p>
    </form>
    <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=[]; 
        $("#tab .tdprice").each(function(){ 
        s.push(parseInt($(this).find('input[class*=text_box]').val())*parseFloat($(this).find('span[class*=price]').text()));
           $(this).find($('.total')).text(parseFloat($(this).find($('.price')).text()*$(this).find($('.text_box')).val()));
        }); 
        
        $("#total").html((s[0]+s[1]).toFixed(2)); 

        } 
        setTotal(); 
    }) 
</script> 
</body> 
</html>