0

我正在构建一个计算器,它可以为房地产交易提供州特定的销售税。我知道我的“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);
            }
        }
    }
};
4

1 回答 1

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.
};

演示

于 2013-03-10T23:36:23.137 回答