HTMLJavascript Page is resetting after submitting form - Stack Overflow

I am working on learning to make forms using HTML and Javascript.When I go to submit my form, it proc

I am working on learning to make forms using HTML and Javascript. When I go to submit my form, it processes it, and I can see the result, but the page quickly resets so that the form is back to its starting state. How can I make sure the page doesn't reset when the function is done?

Here is the HTML for the form and the function that processes it:

<form name="calculator" onsubmit="return calculate(this);">
            Enter the value of the house: $
            <input type="text" name="homeValue" id="homeValue" size="7" /><br>
            <select name="selMortgage" id="selMortgage">
                <option>Select a mortgage length</option>
                <option>15-year mortgage</option>
                <option>30-year mortgage</option>
            </select></br>
            <p class="form"><input type="checkbox" name="chkDiscount" value="discount"/>Good Credit Discount?</p>
            <input type="submit" value="Calculate" />
            <div id="result"></div>
</form>

function calculate(form) {
    var amountEntered = form.homeValue.value;
    var termLength;
    var interestRate;
    var calc;
    document.getElementById("result").innerHTML = "hi";
    if (!/^\d+(\.\d{1,2})?$/.test(amountEntered))
    {
        alert("You did not enter an amount of money");
        //form.homeValue.focus();
        return;
    }
    if (form.chkDiscount.checked == true)
    {
        interestRate = .05;
    }
    else
    {
        interestRate = .06;
    }
    if (form.selMortgage.selectedIndex == 0)
    {
        alert("Select a mortgage length");
    }
    else if (form.selMortgage.selectedIndex == 1)
    {
        termLength = 15;
    }
    else if (form.selMortgage.selectedIndex == 2)
    {
        termLength = 30;
    }
    calc = (Math.pow(1+interestRate,termLength) * amountEntered)/(termLength*12);
    document.getElementById("result").innerHTML = calc.toFixed(2);
}

I am working on learning to make forms using HTML and Javascript. When I go to submit my form, it processes it, and I can see the result, but the page quickly resets so that the form is back to its starting state. How can I make sure the page doesn't reset when the function is done?

Here is the HTML for the form and the function that processes it:

<form name="calculator" onsubmit="return calculate(this);">
            Enter the value of the house: $
            <input type="text" name="homeValue" id="homeValue" size="7" /><br>
            <select name="selMortgage" id="selMortgage">
                <option>Select a mortgage length</option>
                <option>15-year mortgage</option>
                <option>30-year mortgage</option>
            </select></br>
            <p class="form"><input type="checkbox" name="chkDiscount" value="discount"/>Good Credit Discount?</p>
            <input type="submit" value="Calculate" />
            <div id="result"></div>
</form>

function calculate(form) {
    var amountEntered = form.homeValue.value;
    var termLength;
    var interestRate;
    var calc;
    document.getElementById("result").innerHTML = "hi";
    if (!/^\d+(\.\d{1,2})?$/.test(amountEntered))
    {
        alert("You did not enter an amount of money");
        //form.homeValue.focus();
        return;
    }
    if (form.chkDiscount.checked == true)
    {
        interestRate = .05;
    }
    else
    {
        interestRate = .06;
    }
    if (form.selMortgage.selectedIndex == 0)
    {
        alert("Select a mortgage length");
    }
    else if (form.selMortgage.selectedIndex == 1)
    {
        termLength = 15;
    }
    else if (form.selMortgage.selectedIndex == 2)
    {
        termLength = 30;
    }
    calc = (Math.pow(1+interestRate,termLength) * amountEntered)/(termLength*12);
    document.getElementById("result").innerHTML = calc.toFixed(2);
}
Share Improve this question asked Dec 14, 2012 at 4:40 Erik HansonErik Hanson 2491 gold badge4 silver badges12 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

It looks like you are missing a return false.

Since you are submitting the form the values are lost due to page reload. You may try to POST your values then assign the post values on your form elements so that even after refresh youre still able to see the POST data submitted..

Try this:

Just use return false; at the end of your javascript function.

When the form is submitted, the JS function calculate will be invoked. Since the function does not return any value, undefined will be assumed. And in JS the undefined will be assumed to nothing and the form will be submitted to server and reload again. This is why you lose your data. From your code, the page just execute some JS code. So we can prevent submitting the form to keep your data. You may need to return false in calculate function to prevent submitting. Example as below:

document.getElementById("result").innerHTML = calc.toFixed(2); return false;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745225812a4617456.html

相关推荐

  • HTMLJavascript Page is resetting after submitting form - Stack Overflow

    I am working on learning to make forms using HTML and Javascript.When I go to submit my form, it proc

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信