Javascript 报价计算器给出意想不到的输出

标签 javascript html css

这是我的 HTML 代码

   <form id="quoteform" onsubmit="return false;">
            <fieldset>
            <legend>Job Calculator!</legend>
            <p>
            <label for="includecandles" class="inlinelabel">
            Width : </label>
            <input type="text" id="width" name="width" onclick="calculateTotal()" />
            </p>
            <p>
            <label class="inlinelabel" for="includeinscription">
            Height : </label>
            <input type="text" id="height" name="height" onclick="calculateTotal()" />
            </p>

            <br />

            <select id="SurfaceFinishRequired" name="SurfaceFinishRequired" onchange="calculateTotal()">

            <option value="30">Solid(S) x 30</option>
            <option value="55">Flexible (F) x 55</option>
            </select>
            <br />


            <select id="CurrentSurfaceMaterial" name="CurrentSurfaceMaterial" onchange="calculateTotal()">
            <option value="1">Plain Concrete  x 1</option>
            <option value="1.3">Aggregate  Fine 4mm deep x 1.3</option>
            <option value="1.4">Aggregate  Medium 8mm deep x 1.4</option>
            <option value="1.5">Aggregate  Deep 12mm deep x 1.5</option>
            <option value="1.6">Aggregate  Deeo 12mm deep x 1.6</option>
            </select>

            <input type="submit" value="Calculate" class="btn btn-success btn-large pull-right" />
            <div id="totalPrice"></div>
            </fieldset>
        </form>

这是我的 Javascript 代码

function widthfunc()
{

    var width_price=0;



    width_price = document.getElementById("width").value;

    return width_price;

}

function heightfunc()
{

    var height_price=0;

    height_price = document.getElementById("height").value;

    return height_price;

}

function surfacefunc()
{

    var surface_price=0;

    var x = document.getElementById("SurfaceFinishRequired").selectedIndex;

    surface_price = document.getElementsByTagName("option")[x].value;

    return surface_price;

}

function materialfunc()
{

    var material_price=0;



    var y = document.getElementById("CurrentSurfaceMaterial").selectedIndex;

    material_price = document.getElementsByTagName("option")[y].value;

    return material_price;

}


function calculateTotal()
{

    var quote = widthfunc() * heightfunc() * surfacefunc() * materialfunc();

    //display the result
    var divobj = document.getElementById('totalPrice');
    divobj.style.display='block';
    divobj.innerHTML = "Total Price is $"+quote;

}

function hideTotal()
{
    var divobj = document.getElementById('totalPrice');
    divobj.style.display='none';
}

正如我在其他选项选择 1 时测试的那样,宽度和高度工作正常并提供预期的输出。但如果我改变选择选项。它给了我错误的输出。

举个例子:width:1 * Height:1 * SurfaceFinishedRequired : 1 * currentSurfaceMaterial : 1.3 输出是:55。但它应该只是 1.3 (1*1*1*1.3=1.3 )对吗?

最佳答案

而不是使用:

var x = document.getElementById("SurfaceFinishRequired").selectedIndex;
surface_price = document.getElementsByTagName("option")[x].value;

你应该使用:

surface_price = document.getElementById("SurfaceFinishRequired").value;

同样适用

material_price = document.getElementById("CurrentSurfaceMaterial").value;

更新于 Fiddle

关于Javascript 报价计算器给出意想不到的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28582958/

相关文章:

javascript - 异步旅行推销员子旅行的本地搜索启发式

javascript - Metafizzy 同位素 - 进入动画

html - 如何在 Bootstrap 中增加字体大小

javascript - 为具有可变内容的行内 block 元素提供相同的高度?

html - 使 div 适合内部内容

javascript - 带有工厂的 Angular JS 作为本地文件,文件中包含 JSON

javascript - 我如何在 JavaScript 中将 "get"设为成员变量?

javascript - 禁用 OnClick 事件,直到首先触发另一个事件

html - CommonMark 规范是否允许列表标记前有前导空格?

javascript - 我的所有 CSS 文件都被导入到我的 React JS 文件中,但我没有导入它们?