[英]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>
如何获取所有输入值,然后将它们全部添加,然后将其提供给变量。 如果在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;
}
结果将获得所有值。但是我的代码仍然无法工作。我的jquery代码出了什么问题? 如果我不想使用jQuery,请仅使用javascript。 我该怎么办?
这应该适用于jQuery:
var result = "";
$('liste_couleur_qty li input').each(function() {
result += $(this).val();
});
首先: jQuery.val
不是属性,而是方法。 第二:它将返回结果集中第一个元素的值,而不是所有值。 第三:在您的for循环中-我什至猜不到您要做什么。 您始终使用相同的值length
时间。 另外,一旦尝试获取数值,则应使用undefined提到的parseInt/parseFloat
方法
var result = 0;
$('.liste_couleur_qty li input').each(function(){
result += $(this).val();
});
console.log(result);
这并不完全准确。 正如@undefined指出的那样,您需要使用parseInt
来执行数字加法运算而不是串联运算。 那是:
result += parseInt($(this).val());
您的选择器未选择任何内容,您错过了.
对于类选择器,同样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.