简体   繁体   中英

How to get the input value all and make an addition

    <ul class="liste_couleur_qty">
         <li style="margin-bottom: 20px;">
                <dl>
                        <table width="200" border="0">
                      <tbody><tr>
                        <td width="50%">
                        <span style="display: block; font-size: 13px; line-height: 16px; margin-bottom: 2px;margin-right: 0px;margin-left: 20px;">Noir</span>
                    </td>

                        <td width="50%"><div class="add-to-cart">

                          <label for="qty-2195">qty :</label>

                          <input type="text" class="input-text qty calcul_qty_product" title="Qté" value="0" autocomplete="off" maxlength="5" data-product_color="127" id="qty-2195" name="qty-2195" onblur="addToCartPlus(2195, 127, this);">

                        </div></td>
                      </tr>
                    </tbody></table>
                    </dl>
            </li>

            <li style="margin-bottom: 20px;">
                <dl>
                        <table width="200" border="0">
                      <tbody><tr>
                        <td width="50%">
                        <span style="display: block; font-size: 13px; line-height: 16px; margin-bottom: 2px;margin-right: 0px;margin-left: 20px;">Blanc</span>
                    </td>

                        <td width="50%"><div class="add-to-cart">

                          <label for="qty-2196">qty :</label>

                          <input type="text" class="input-text qty calcul_qty_product" title="Qté" value="0" autocomplete="off" maxlength="5"  id="qty-2196" name="qty-2196" onblur="addToCartPlus();">

                        </div></td>
                      </tr>
                    </tbody></table>
                    </dl>
            </li>
<li style="margin-bottom: 20px;">
                <dl>
                        <table width="200" border="0">
                      <tbody><tr>
                        <td width="50%">
                        <span style="display: block; font-size: 13px; line-height: 16px; margin-bottom: 2px;margin-right: 0px;margin-left: 20px;">Blanc</span>
                    </td>

                        <td width="50%"><div class="add-to-cart">

                          <label for="qty-2196">qty :</label>

                          <input type="text" class="input-text qty calcul_qty_product" title="Qté" value="0" autocomplete="off" maxlength="5"  id="qty-2196" name="qty-2196" onblur="addToCartPlus();">

                        </div></td>
                      </tr>
                    </tbody></table>
                    </dl>
            </li>

                </ul> 

how to get all the input value and then add them all then give it to a variable. if in jquery,i know how to do.

var length = $('liste_couleur_qty li input').length;
var input = $('liste_couleur_qty li input').val;
for(var i=0;i< length;i++){
  var result += input;
}

the result will get all the value all.but my code still can't work.what's wrong with my jquery code? if i not want to use jquery only use javascript. how do i do?

This should work with jQuery:

var result = "";
$('liste_couleur_qty li input').each(function() {
   result += $(this).val();
});

First: jQuery.val is not a property, but a method. Second: it will return value of first element in result set, not all values. Third: in your for loop - I can't even guess what are you trying to do. You are always using the same value length times. Also, once you are trying to get numerical value, you should use parseInt/parseFloat methods as mentioned by undefined

var result = 0;
$('.liste_couleur_qty li input').each(function(){
    result += $(this).val();
});
console.log(result);

This is not entirely accurate. As @undefined has pointed out, you need to use parseInt to get perform numeric addition instead of concatenation. That is:

result += parseInt($(this).val());

Your selector selects nothing, you have missed the . for the class selector, also val is a method not a property, you can use each method and parseInt function:

var result = 0;
$('.liste_couleur_qty li input').each(function(){
     result += parseInt(this.value, 10);
});
var length = $('liste_couleur_qty li input').length;
var input = $('liste_couleur_qty li input').val();
for(var i=0;i< length;i++){
  var result += input;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM