JavaScript_shoping++

<html>
<head>
<title>Shopping</title>
<script>
    function buy(row){ 
        var flag =true;
        var table = document.getElementById("table1");
        var num =prompt("请输入需要购买的数量:","0");
        if(num){
            if(isNaN(num)){
                alert("请输入一个数字类型整数");
            }else if(num==0||num=="undefined"||num=="null"){
                alert("输入不能为空");
            }else{        
     //    alert(row.cells[1].innerHTML=="苹果");//true
//           alert(table.rows.length);
                for(var i =0;i<table.rows.length;i+=1){
          
                    if(table.rows[i].cells[0].innerHTML==row.cells[1].innerHTML){
                        var num1 = table.rows[i].cells[1].innerHTML;
                    //点击购买-弹出框后 点取消还是会输入一行,值为null
//                        if(num1=="null")
//                            num1=0;                   
                        table.rows[i].cells[1].innerHTML=null;
                        num2 =parseInt(num1);
                        num3 = parseInt(num);
                        num4 = num3+num2;
                        table.rows[i].cells[1].innerHTML = num4; 
                        flag = false;
                        break;             
                    }
                }
                if(flag){
                var newrow = document.getElementById("table1").insertRow(-1);
                    newrow.insertCell(0).innerHTML = row.cells[1].innerHTML;
                    newrow.insertCell(1).innerHTML = num;
                    count1 = parseInt(num);   
                }
            }
        }
    }
    function payfor(){
        var pay1 = 0;
        var pay2 = 0;
        var pay3 = 0;
        var jiage01 = 2;
        var jiage02 = 3;
        var jiage03 = 5;
        var table =document.getElementById("table1");
        for(var i=0;i<table.rows.length;i+=1){
            if(table.rows[i].cells[0].innerHTML=="苹果"){
              
               var one = table.rows[i].cells[1].innerHTML;
            
                pay1 =parseInt(one)*jiage01;
            }
            if(table.rows[i].cells[0].innerHTML=="香蕉"){
               var two = table.rows[i].cells[1].innerHTML;
                pay2 =parseInt(two)*jiage02;
            }
            
            if(table.rows[i].cells[0].innerHTML=="葡萄"){
               var three = table.rows[i].cells[1].innerHTML;
                pay3 =parseInt(three)*jiage03;
            }
        }
        var allpay = pay1+pay2+pay3;
        alert("你购买的商品总价为:"+allpay+"元,欢迎下次光临!");
        var r = confirm("是否需要清空购物车?");
        if(r){
            for(var j=table1.rows.length-1;j>0;j--){
                table.deleteRow(j);       
            }
        }
   
    }
</script>
</head>
<body>
<hr>
    <table border="1" cellpadding="1" cellspacing="1" id="table1" width="50%" align="center">
        购物车:<input type="button" value="结帐" onclick="payfor()">
        <thead align="center"><td>商品名称</td><td>购买数量</td></thead>
    </table>
<hr>
<table id="table2" border="1" width="50%" align="center">
    <thead align="center"><td>编号</td><td>商品名称</td><td>商品单价</td><td>操作</td></thead>
    <tr align="center"><td>1</td><td>苹果</td><td>2.0元/斤</td><td><input type="button" value="购买" onclick="buy(this.parentNode.parentNode)">    </td></tr>
    <tr align="center"><td>2</td><td>香蕉</td><td>3.0元/斤</td><td><input type="button" value="购买" onclick="buy(this.parentNode.parentNode)"></tr>
    <tr align="center"><td>3</td><td>葡萄</td><td>5.0元/斤</td><td><input type="button" value="购买" onclick="buy(this.parentNode.parentNode)"></tr>
</table>

</body>
</html>

你可能感兴趣的:(JavaScript,职场,休闲)