jQuery实现购物车物品数量的加减 (针对GirdView的类似事件)

<!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>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>jQuery实现购物车物品数量的加减</title>  
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>  
<script>  
$(function(){  
    var t = $("#text_box");  
    $("#add").click(function(){       
        t.val(parseInt(t.val())+1)  
        setTotal();  
    })  
    $("#min").click(function(){  
        t.val(parseInt(t.val())-1)  
        setTotal();  
    })  
    function setTotal(){  
        $("#total").html((parseInt(t.val())*3.95).toFixed(2));  
    }  
    setTotal();  
})  
</script>  
</head>  
<body>  
<p>单价:3.95</p>  
<input id="min" name="" type="button" value="-" />  
<input id="text_box" name="" type="text" value="4" />  
<input id="add" name="" type="button" value="+" />  
<p>总价:<label id="total"></label></p>  
</body>  
</html> 

 下面的是自己写的针对GirdView的事件

     function IncNumber() {
            var e = event.srcElement;//获取鼠标点击的元素
            var r = e.parentElement.parentElement.rowIndex - 1;//获取元素所在的行的行
            //ctl00$ContentPlaceHolder1$GridView_PurchaseOrder$ctl02$txtRemarks
            //ctl00$ContentPlaceHolder1$GridView_PurchaseOrder$ctl03$txtRemarks
            //ContentPlaceHolder1_GridView_PurchaseOrder_txtRemarks_2
            var numberid = 'GridView_Goods_lblNumber_' + r;//获取TextBox的ID,由于TextBox是在Gridview里面的,所以他的
            //ID会变成这样的格式,GridView的ID加上控件所在的行号加上TextBox的ID 
            //GridView_Goods_lblNumber_1
            //ContentPlaceHolder1_GridView_PurchaseOrder_txtRemarks_
            var lblNumber = document.getElementById("" + numberid + "");
            lblNumber.innerText = parseInt(lblNumber.innerText) + 1;

            var priceid = 'GridView_Goods_lblPrice_' + r;
            var lblPriceid = document.getElementById("" + priceid + "");

            var amountid = 'GridView_Goods_lblAmount_' + r;
            var lblAmount = document.getElementById("" + amountid + "");
            lblAmount.innerText = parseFloat(lblPriceid.innerText) * parseInt(lblNumber.innerText);
            return false;
   
        }
        function SubNumber() {
            var e = event.srcElement;//获取鼠标点击的元素
            var r = e.parentElement.parentElement.rowIndex - 1;//获取元素所在的行的行
           
            var numberid = 'GridView_Goods_lblNumber_' + r;
            var lblNumber = document.getElementById("" + numberid + "");
            if (parseInt(lblNumber.innerText) > 0) {
                lblNumber.innerText = parseInt(lblNumber.innerText) - 1;

                var priceid = 'GridView_Goods_lblPrice_' + r;
                var lblPriceid = document.getElementById("" + priceid + "");

                var amountid = 'GridView_Goods_lblAmount_' + r;
                var lblAmount = document.getElementById("" + amountid + "");
                lblAmount.innerText = parseFloat(lblPriceid.innerText) * parseInt(lblNumber.innerText);
            }   
            return false;
        }

 

posted on 2013-09-06 10:42  YoungPop_Chen  阅读(489)  评论(0编辑  收藏  举报

导航