亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

javascript développe une fonction de panier d'achat pour implémenter la fonction de signe plus

Jetons un ?il à la partie html du code?:

<!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>
</head>
<body>
        <table cellspacing="0" cellpadding="0" border="1">
            <tr>
                <th>名稱</th>
                <th>單價(jià)</th>
                <th>數(shù)量</th>
                <th>總價(jià)</th>
            </tr>

            <tr class="tr2">
                <td>手表</td>
                <td id="price">1999</td>
                <td>
                    <a href="#" id="a1" class="tp1">-</a>
                    <input type="text" value="1" id="count" >
                    <a href="#" id="a2" class="tp2">+</a>
                </td>
                <td id="total">1999</td>
            </tr>

            <tr class="tr2">
                <td>手機(jī)</td>
                <td id="price_1">2000</td>
                <td>
                    <a href="#" id="a1" class="tp1">-</a>
                    <input type="text" value="1" id="count_1">
                    <a href="#" id="a2" class="tp2">+</a>
                </td>
                <td id="total_1">2000</td>
            </tr>
        </table>
        </br>
</body>
</html>

Comme le montre le code ci-dessus, nous avons 2 produits si nous ajoutons un produit à chaque fois et écrivons la fonction plus et moins, alors. le code sera Il y en a trop. Bien que ce soit possible, ce n'est pas conseillé, nous pouvons donc créer une fonction ci-dessous

Analysons, quels sont les paramètres de la fonction ?

En fait, si nous faisons la fonction d'addition et de soustraction, puis que le prix total change, tout ce dont nous avons besoin sont 3 paramètres, prix unitaire, prix total, quantité

Regardez la cellule affichant le prix unitaire dans le code ci-dessus est le prix total de la cellule, l'identifiant est l'identifiant total de la première zone de texte du produit est le nombre

Ensuite, nous regardons le code de la cellule?:

<!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>
</head>
<body>
        <table cellspacing="0" cellpadding="0" border="1">
            <tr>
                <th>名稱</th>
                <th>單價(jià)</th>
                <th>數(shù)量</th>
                <th>總價(jià)</th>
            </tr>

            <tr class="tr2">
                <td>手表</td>
                <td id="price">1999</td>
                <td>
                    <a href="#" id="a1" class="tp1" onclick="a2('price','total','count')">-</a>
                    <input type="text" value="1" id="count" onblur="a3('price','total','count')">
                    <a href="#" id="a2" class="tp2" onclick="a1('price','total','count')">+</a>
                </td>
                <td id="total">1999</td>
            </tr>

            <tr class="tr2">
                <td>手機(jī)</td>
                <td id="price_1">1999</td>
                <td>
                    <a href="#" id="a1" class="tp1" onclick="a2('price_1','total_1','count_1')">-</a>
                    <input type="text" value="1" id="count_1" onblur="a3('price_1','total_1','count_1')">
                    <a href="#" id="a2" class="tp2" onclick="a1('price_1','total_1','count_1')">+</a>
                </td>
                <td id="total_1">1999</td>
            </tr>
        </table>
        </br>
</body>
</html>

As. au-dessus du code, nous ajoutons Le signe moins est lié à un événement de clic, qui contient 3 paramètres?: prix unitaire du produit prix et prix_1 prix total total et total_1 quantité nombre et nombre_1

Implémentons la fonction de signe plus

<script type="text/javascript">
            function a1(td,td2,id){
                var price = document.getElementById(td).innerHTML;//獲得單價(jià)
                var total = document.getElementById(td2).innerHTML;//獲得總價(jià)
                var v1 = parseInt(document.getElementById(id).value);//獲得數(shù)量
                document.getElementById(id).value = v1+1;   //獲取點(diǎn)擊后的數(shù)量
                document.getElementById(td2).innerHTML = parseInt(price) * parseInt(v1+1); //總價(jià)格等于單價(jià)乘以點(diǎn)擊后的數(shù)量值
            }
</script>

Jetons un coup d'?il au code complet suivant?: Voyons comment fonctionne l'implémentation

<!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">
        function a1(td,td2,id){
                var price = document.getElementById(td).innerHTML;//獲得單價(jià)
                var total = document.getElementById(td2).innerHTML;//獲得總價(jià)
                var v1 = parseInt(document.getElementById(id).value);//獲得數(shù)量
                document.getElementById(id).value = v1+1;
                document.getElementById(td2).innerHTML = parseInt(price) * parseInt(v1+1);
            }
    </script>
</head>
<body>
        <table cellspacing="0" cellpadding="0" border="1">
            <tr>
                <th>名稱</th>
                <th>單價(jià)</th>
                <th>數(shù)量</th>
                <th>總價(jià)</th>
            </tr>

            <tr class="tr2">
                <td>手表</td>
                <td id="price">1999</td>
                <td>
                    <a href="#" id="a1" class="tp1" onclick="a2('price','total','count')">-</a>
                    <input type="text" value="1" id="count" onblur="a3('price','total','count')">
                    <a href="#" id="a2" class="tp2" onclick="a1('price','total','count')">+</a>
                </td>
                <td id="total">1999</td>
            </tr>

            <tr class="tr2">
                <td>手機(jī)</td>
                <td id="price_1">1999</td>
                <td>
                    <a href="#" id="a1" class="tp1" onclick="a2('price_1','total_1','count_1')">-</a>
                    <input type="text" value="1" id="count_1" onblur="a3('price_1','total_1','count_1')">
                    <a href="#" id="a2" class="tp2" onclick="a1('price_1','total_1','count_1')">+</a>
                </td>
                <td id="total_1">1999</td>
            </tr>
        </table>
        </br>
</body>
</html>

Regardez l'exemple ci-dessus, peu importe le nombre de produits que j'ai, si je clique sur le signe plus, le prix sera différent et le prix total sera différent, alors utilisez ceci La méthode sera plus pratique et plus simple

Formation continue
||
<!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"> function a1(td,td2,id){ var price = document.getElementById(td).innerHTML;//獲得單價(jià) var total = document.getElementById(td2).innerHTML;//獲得總價(jià) var v1 = parseInt(document.getElementById(id).value);//獲得數(shù)量 document.getElementById(id).value = v1+1; document.getElementById(td2).innerHTML = parseInt(price) * parseInt(v1+1); } </script> </head> <body> <table cellspacing="0" cellpadding="0" border="1"> <tr> <th>名稱</th> <th>單價(jià)</th> <th>數(shù)量</th> <th>總價(jià)</th> </tr> <tr class="tr2"> <td>手表</td> <td id="price">1999</td> <td> <a href="#" id="a1" class="tp1" onclick="a2('price','total','count')">-</a> <input type="text" value="1" id="count" onblur="a3('price','total','count')"> <a href="#" id="a2" class="tp2" onclick="a1('price','total','count')">+</a> </td> <td id="total">1999</td> </tr> <tr class="tr2"> <td>手機(jī)</td> <td id="price_1">1999</td> <td> <a href="#" id="a1" class="tp1" onclick="a2('price_1','total_1','count_1')">-</a> <input type="text" value="1" id="count_1" onblur="a3('price_1','total_1','count_1')"> <a href="#" id="a2" class="tp2" onclick="a1('price_1','total_1','count_1')">+</a> </td> <td id="total_1">1999</td> </tr> </table> </br> </body> </html>
soumettreRéinitialiser le code