使用Javascript和HTML5输出,但没有得到输出

时间:2013-03-10 23:22:47

标签: javascript html5

我正在建立一个计算器,用于给出房地产交易的州特定销售税。我知道我的" normalrtfCalc"功能有效,但我的问题是获得"金额"从形式到功能和结果到输出。任何帮助将不胜感激。 谢谢!

这是我的HTML:

<form id="rtfCalc" oninput="updateOutput( )">
    <input name="sale amount" type="number" value="0" />
    <output name="transfer fee" for="sale amount"></output>
</form>

这是我的JS:

function updateOutput() {
    var form = document.getElementById("rtfCalc");
    var out = form.elements["transfer fee"];
    var amount = parseInt(form.elements["sale amount"].value);

    function normalrtfCalc(amount) {
        if (amount <= 150000) {
            out.value = Math.ceil(amount / 500) * 2;
        } else if (amount <= 350000) {
            if ((amount - 150000) <= 50000) {
                out.value = 600 + (Math.ceil((amount - 150000) / 500) * 3.35);
            } else {
                out.value = 935 + (Math.ceil((amount - 200000) / 500) * 3.9);
            }
        } else {
            if ((amount - 200000) <= 350000) {
                out.value = 2735 + (Math.ceil((amount - 200000) / 500) * 4.8);
            } else if ((amount - 550000) <= 300000) {
                out.value = 4655 + (Math.ceil((amount - 555000) / 500) * 5.3);
            } else if ((amount - 850000) <= 150000) {
                out.value = 7835 + (Math.ceil((amount - 850000) / 500) * 5.8);
            } else {
                out.value = 9575 + (Math.ceil((amount - 1000000) / 500) * 6.05);
            }
        }
    }
};

1 个答案:

答案 0 :(得分:1)

您的代码有几个问题。我将在下面发布并在评论中解释:

function updateOutput() {
    var form = document.getElementById("rtfCalc");
    var out = form.elements["transfer_fee"];
    var amount = parseInt(form.elements["sale_amount"].value);

    function normalrtfCalc(amount) { // an equal sign(=) before the opening curly bracket is invalid syntax; remove it, and execute the function as stated below, and your code works.

        if (amount <= 150000) {
            out.value = Math.ceil(amount / 500) * 2;

        } else if (amount <= 350000) {
            if ((amount - 150000) <= 50000) {
                out.value = 600 + (Math.ceil((amount - 150000) / 500) * 3.35);
            } else {
                out.value = 935 + (Math.ceil((amount - 200000) / 500) * 3.9);
            }
        } else {
            if ((amount - 200000) <= 350000) {
                out.value = 2735 + (Math.ceil((amount - 200000) / 500) * 4.8);
            } else if ((amount - 550000) <= 300000) {
                out.value = 4655 + (Math.ceil((amount - 555000) / 500) * 5.3);
            } else if ((amount - 850000) <= 150000) {
                out.value = 7835 + (Math.ceil((amount - 850000) / 500) * 5.8);
            } else {
                out.value = 9575 + (Math.ceil((amount - 1000000) / 500) * 6.05);
            }
        }
    }
    normalrtfCalc(amount); //you have to call the function in order for it to execute.
};

DEMO