计算音量

时间:2016-08-28 09:51:41

标签: javascript

我想创建类似here.的类似计算器。它计算覆盖物的体积。访客插入三个数字:宽度,长度和高度(厚度),它表示需要购买多少。

HTML:

<form method="post" accept-charset="UTF-8">
<div class="form_area">

    <div class="form_fields">
        <div class="form_field   ">
            <label class="form_field_label">Width (m)</label>
            <input type="text" value="" name="laius" id="laius" rel="calc" class="form_field_textfield form_field_size_medium">
        </div>

        <div class="form_field   ">
            <label class="form_field_label">Length (m)</label>
            <input type="text" value="" name="pikkus" id="pikkus" rel="calc" class="form_field_textfield form_field_size_medium">
        </div>

        <div class="form_field   ">
            <label class="form_field_label">Thickness (cm)</label>
            <input type="text" value="" name="paksus" id="paksus" rel="calc" class="form_field_textfield form_field_size_medium">
        </div>

        <div class="form_field   ">
            <label class="form_field_label">Total (m<sup>3</sup>)</label>
            <input type="text" value="" readonly name="kokku" id="kokku" class="form_field_textfield form_field_size_small">
        </div>
    </div>

    <div class="form_submit">
        <input type="submit" value="Arvuta" id="calc_submit" name="commit">
    </div>
</div>
</form>

的javascript:

    ! function($) {
    $(function() {
        $("[rel='calc']").arvutus();
    });

    $.fn.arvutus = function() {
        var inputs = $(this);
        var kokku = $("#kokku:first");

        inputs.bind("change keyup", function() {

            var obj = $(this);

            if (obj.val() !== "") {

                parseFloat(obj.val()).toFixed(2);
            };
            arvuta();
        });

        $("#calc_submit").bind("click", function(e) {
            e.preventDefault();
            arvuta();
        });

        function arvuta() {
            var width = inputs.filter("#laius").val();
            width = width.toString();
            width = width.replace(",", ".");
            var lenght = inputs.filter("#pikkus").val();
            lenght = lenght.toString();
            lenght = lenght.replace(",", ".");
            var thickness = inputs.filter("#paksus").val();
            thickness = thickness.toString();
            thickness = thickness.replace(",", ".");
            thickness = thickness / 100;
            var sum = width * lenght * thickness
            sum = sum.toFixed(2);
            kokku.val(sum + " m3 multši.");
        };
    };
  }(window.jQuery);

我将html和javascript插入jsfiddle,但我的不起作用。可能我想念一些非常明显的东西。一些更多的JavaScript?

不久之前,

更新:,有人提供了一个正常工作的代码,但版主很快将其删除了,我无法复制它...... :(

1 个答案:

答案 0 :(得分:1)

代码parseFloat(obj.val()).toFixed(2)会返回一个值,但您没有对其执行任何操作。它应该存储在某个地方,以便您可以用它来计算音量吗?