开发者

jQuery Calculation Plugin - Simple Advice Needed

Simple question, but I can't figure it out.

What is the launcher syntax I need to perform the following function:

<input type="text" id="value1" />
<input type="text" id="value2" />开发者_运维百科;
<input type="text" id="value3" />
<input type="text" id="total" />

I want to perform [(value1 * value2) + value3] and have the result rounded to two decimal places (forced, so even if the result is a round integer, it should append ".00") and displayed in total. And I need this to happen on a keyup action for any of the value fields.

Should be very straightforward, but I just can't figure it out.

Help please?


$(function() {
    var roundDecimals = 2;
    var pow = Math.pow(10, roundDecimals);

    $('input[id^=value]').keyup(function() {
        var value1 = parseFloat($('#value1').val());
        var value2 = parseFloat($('#value2').val());
        var value3 = parseFloat($('#value3').val());

        if (isNaN(value1) || isNaN(value2) || isNaN(value3)) {
            return;
        }

        var total = value1 * value2 + value3;
        total = Math.round(total * pow) / pow;
        $('#total').val(total.toFixed(roundDecimals));
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜