简体   繁体   English

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

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

Here is my code: 这是我的代码:

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

What I am trying to do is enter in the number with formatting within each textbox and then I want to get the sum of which will be the total of all the textboxes. 我想做的是在每个文本框内输入带有格式的数字,然后我想得到的总和将是所有文本框的总数。 What am I doing wrong here. 我在这里做错了。 I thought I pretty much had it but all I am getting for an output is 0.00. 我以为我已经拥有了,但是我得到的输出是0.00。 Whats wrong here is the .total that is not giving the right output. 这里的问题是.total没有提供正确的输出。 What can I do to fix this so it works correctly? 我该怎么做才能使它正常工作? Please help. 请帮忙。 Thanks. 谢谢。

My guess is you want this: 我的猜测是您想要这样:

HTML: HTML:

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

JavaScript: 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(); 
});

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

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

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