javascript開發購物車教學之功能完善

前面2節,我們已經把購物車加減,總價發生變化的功能已經完成,但是還是有一些小問題的

文本框內我如果輸入文字,英文,這樣我們就需要來進行處理了

小夥伴們思考一下,在文字框輸入內容,當我們滑鼠離開文字框,就會觸發失去焦點事件,下面我們來做這塊的處理就會用到  onblur失去焦點事件,

如果我們輸入的是數字,那麼總價是也需要根據我們輸入的數字來進行計算的

下面我們來寫失去焦點事件,代碼如下:

<script>
    document.getElementById('id').onblur = function(){
            var id = document.getElementById('id').value;
            //判断是否是数字或者是否小于1
             if(isNaN(id) || id < 1){
                  alert("请输入正确的数字");
                  document.getElementById('id').value = 1;
                  return;
              }
             document.getElementById('td2').innerHTML = parseInt(id) * parseInt(good);
       }

如上程式碼:當文字的內容不是數字,或小於1的時候, 會提示,然後不往下執行,直接回傳

所以這塊的功能我們也已經完成

完整程式碼如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
        table{width:350px;border:1px solid #eee;text-align:center;}
        .tr2{height:50px;}
        input{width:30px;height:20px;text-align: center;}
        a{text-decoration:none}
    </style>
    <script type="text/javascript">
            window.onload=function(){
                var input = document.getElementById('id').value; //获取文本框的value值
                var good = document.getElementById('td').innerHTML; //获取id是td的html文本内容
                //加号功能
                document.getElementById('a2').onclick = function(){                            
                            var v1 = document.getElementById('id').value;
                            v1=parseInt(v1);
                            document.getElementById('id').value = v1 + 1;
                            document.getElementById('td2').innerHTML = parseInt(good) * parseInt(v1+1);
                }
                //减号功能
                document.getElementById('a1').onclick = function(){                            
                            var v1 = document.getElementById('id').value;
                            if(v1>1){
                                v1=parseInt(v1);
                                document.getElementById('id').value = v1 - 1;
                                document.getElementById('td2').innerHTML = parseInt(good) * parseInt(v1-1);
                            }else{
                                document.getElementById('id').value=1;
                            }
                }

                document.getElementById('id').onblur = function(){
                    var id = document.getElementById('id').value;
                    //判断是否是数字或者是否小于1
                    if(isNaN(id) || id < 1){
                        alert("请输入数字");
                        document.getElementById('id').value = 1;
                        return;
                    }
                    document.getElementById('td2').innerHTML = parseInt(id) * parseInt(good);
                }


            }
    </script>
</head>
<body>
        <table cellspacing="0" cellpadding="0" border="1">
            <tr>
                <th>名称</th>
                <th>单价</th>
                <th>数量</th>
                <th>总价</th>
            </tr>

            <tr class="tr2">
                <td>手表</td>
                <td id="td">1999</td>
                <td>
                    <a href="#" id="a1" class="tp1">-</a>
                    <input type="text" value="1" id="id">
                    <a href="#" id="a2" class="tp2">+</a>
                </td>
                <td id="td2">1999</td>
            </tr>
        </table>
</body>
</html>


#
繼續學習
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> table{width:350px;border:1px solid #eee;text-align:center;} .tr2{height:50px;} input{width:30px;height:20px;text-align: center;} a{text-decoration:none} </style> <script type="text/javascript"> window.onload=function(){ var input = document.getElementById('id').value; //获取文本框的value值 var good = document.getElementById('td').innerHTML; //获取id是td的html文本内容 //加号功能 document.getElementById('a2').onclick = function(){ var v1 = document.getElementById('id').value; v1=parseInt(v1); document.getElementById('id').value = v1 + 1; document.getElementById('td2').innerHTML = parseInt(good) * parseInt(v1+1); } //减号功能 document.getElementById('a1').onclick = function(){ var v1 = document.getElementById('id').value; if(v1>1){ v1=parseInt(v1); document.getElementById('id').value = v1 - 1; document.getElementById('td2').innerHTML = parseInt(good) * parseInt(v1-1); }else{ document.getElementById('id').value=1; } } document.getElementById('id').onblur = function(){ var id = document.getElementById('id').value; //判断是否是数字或者是否小于1 if(isNaN(id) || id < 1){ alert("请输入数字"); document.getElementById('id').value = 1; return; } document.getElementById('td2').innerHTML = parseInt(id) * parseInt(good); } } </script> </head> <body> <table cellspacing="0" cellpadding="0" border="1"> <tr> <th>名称</th> <th>单价</th> <th>数量</th> <th>总价</th> </tr> <tr class="tr2"> <td>手表</td> <td id="td">1999</td> <td> <a href="#" id="a1" class="tp1">-</a> <input type="text" value="1" id="id"> <a href="#" id="a2" class="tp2">+</a> </td> <td id="td2">1999</td> </tr> </table> </body> </html>