简体   繁体   English

如何获得所有输入值并进行加法运算

[英]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. 如果在jQuery中,我知道该怎么办。

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? 结果将获得所有值。但是我的代码仍然无法工作。我的jquery代码出了什么问题? if i not want to use jquery only use javascript. 如果我不想使用jQuery,请仅使用javascript。 how do i do? 我该怎么办?

This should work with jQuery: 这应该适用于jQuery:

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

First: jQuery.val is not a property, but a method. 首先: jQuery.val不是属性,而是方法。 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. 第三:在您的for循环中-我什至猜不到您要做什么。 You are always using the same value length times. 您始终使用相同的值length时间。 Also, once you are trying to get numerical value, you should use parseInt/parseFloat methods as mentioned by undefined 另外,一旦尝试获取数值,则应使用undefined提到的parseInt/parseFloat方法

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. 正如@undefined指出的那样,您需要使用parseInt来执行数字加法运算而不是串联运算。 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: 对于类选择器,同样val是方法而不是属性,可以使用each方法和parseInt函数:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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