javascript 提交后表单停留在同一页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26189025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:38:19  来源:igfitidea点击:

Form stay on same page after submit

javascriptformssubmit

提问by turd.ferguson

My page keeps submitting even after it shows the alert message and the return false code is clearly present. What am I doing wrong here?

我的页面即使在显示警报消息并且明确存在返回错误代码后仍继续提交。我在这里做错了什么?

     function validateForm () {
        var selected = "";
        var radios = document.getElementsByName("special");
        var len = document.getElementsByName("special").length;
        var i;

        for (i = 0; i < len; i++) {
            if(radios[i].checked) {
                selected = radios[i].value;
                break;
            }
            if(selected == "") {
                alert("Must select option.");
                return false;
            }
            else {
                return true;
            }
        }
     }

    <form action="FormProcessor.html" method="post" onreset="blank();" onsubmit="validateForm();" name="myForm">
            <p>Would you like special offers sent to you e-mail?</p>
            <input type="radio" name="special" value="Yes"/>Yes
            <input type="radio" name="special" value="No"/>No<br/>
            <input type="submit"/>
            <input type="reset"/>
    </form>

回答by Mritunjay

You have to just say like this

你只能这样说

onsubmit="return validateForm();"

instead of

代替

onsubmit="validateForm();"

updated demo

更新演示

That was because you were checking the value of selectedin forloop, so for first time it's value is ""second time it's become No.

那是因为您正在检查selectedinfor循环的值,所以第一次它的值是""第二次它变成No.

回答by Trott

In your submit handler, instead of return false, use event.preventDefault.

在您的提交处理程序中return false,使用event.preventDefault.

function validateForm (event) {
...
        if(selected == "") {
            alert("Must select option.");
            event.preventDefault();
        }

You'll have to do something more involved to support IE8.

您必须做更多的事情来支持 IE8。

event.preventDefault ? event.preventDefault() : event.returnValue = false;

Alternatively, you can change the onsubmitattribute to be return validateForm(). That will both prevent the default action and stop the event propagation, rather than just prevent the default action.

或者,您可以将onsubmit属性更改为return validateForm()。这将阻止默认操作并停止事件传播,而不仅仅是阻止默认操作。

回答by Deepak anand

You have missed to write return before the onsubmit() method in form.

您错过了在表单中的 onsubmit() 方法之前编写 return 。

onsubmit="return validateForm();"

When you don't write return before the method call of onSubmit() then the javascript can not return the page back into the form.

当您在 onSubmit() 方法调用之前不写 return 时,javascript 无法将页面返回到表单中。