JQuery函数格式和求和或2个文本框的问题

Issue with JQuery Function Formatting and sum or 2 text boxes

本文关键字:文本 问题 2个 函数 格式 求和 JQuery      更新时间:2023-09-26

下面是我的代码:

<script src="Scripts/jquery.formatCurrency-1.4.0.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $(".toCalculate").blur(function () {
            var total = 0;
            $(".toCalculate").formatCurrency(function (index, item) {
                temp = parseFloat($(item).val());
                if (isNaN(temp))
                    temp = 0;
                total = total + temp;
            });
            $(".total").val(total.toFixed(2));
        });
    });
</script>

我要做的是在每个文本框内输入格式的数字,然后我想得到的总和将是所有文本框的总和。我做错了什么?我以为我差不多有了,但我得到的输出是0.00。这里的问题是total没有给出正确的输出。我能做些什么来解决这个问题,使其正常工作?请帮助。谢谢。

我猜你想要这个:

HTML:

<input class="toCalculate" /><br/>
<input class="toCalculate" /><br/>
<input class="toCalculate" /><br/>
<input class="toCalculate" /><br/>
<hr/>
<input class="total">
JavaScript:

$(".toCalculate").on("blur", function(){    
    var total = 0;
    $(".toCalculate").each(function (index, item) {
        var temp = parseFloat($(this).val().replace(/[,$]/g,""));
        if (isNaN(temp))
            temp = 0;
        total = total + temp;
    }).formatCurrency();
    $(".total").val(total.toFixed(2)).formatCurrency(); 
});

示例:http://jsfiddle.net/C448Z/1/