获取所有输入值并进行加法 [英] get all the input value and make an addition

查看:87
本文介绍了获取所有输入值并进行加法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    <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> 
<div id="qtyvalue"><div>

我想做

动态更改 div(qtyvalue)的内容?如果还有更多输入文字,请将它们加在一起,然后在 div(qtyvalue)中显示数字.我使用以下代码.

change the content of a div(qtyvalue) dynamically when the input value changes? if there are more input text vaule, add them together then shows the number in the div(qtyvalue). i using the following code.

input.onkeyup = function() {
var result = 0;
$('.liste_couleur_qty li input').each(function(){
     result += parseInt(this.value, 10);
});
    document.getElementById('qtyvalue').innerHTML = result.value;    
}

但是代码不起作用,如果有两个或多个输入文本框,我不知道如何循环输入.谢谢.

but the code doesn't work, i don't know how to loop the input,if there are two or many input text box. thank you.

推荐答案

您想要的是什么

$(document).ready(function() { //wrap in a document.ready event handler
    $('input').on('keyup', function() { //bind using jQuery
        var result = 0;
        $('.liste_couleur_qty li input').each(function() {
            result += parseInt(this.value, 10);
        });
        $('div#qtyvalue').text(result); //result.value doesn't exist, use result.
    });
});​

这是一个演示: http://jsfiddle.net/jeRdA/

UDPATE:

要允许用户将任何输入的值更改为''(例如,空白或空)或非数字值,请修改以下行:

To allow for users changing the value of any of the inputs to ''(e.g., blank, or empty) or a non-numeric value, modify the line:

result += parseInt(this.value, 10);

收件人:

result += parseFloat(this.value, 10) || 0;

更新的提琴: http://jsfiddle.net/jeRdA/3/

这篇关于获取所有输入值并进行加法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆