繁体   English   中英

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

[英]Issue with JQuery Function Formatting and sum or 2 text boxes

这是我的代码:

<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/

暂无
暂无

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

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