简体   繁体   English

即使onsubmit函数返回false仍在提交表单

[英]form is being submitted even if onsubmit function returns false

So I have a form that looks like this : 所以我有一个看起来像这样的表格:

<form class="form-horizontal" method="POST" onSubmit="submitFormButton()">

and my submitformbutton function : 和我的Submitformbutton函数:

function submitFormButton(){
    document.getElementById('vacationApplicationForm').action = '';
    document.getElementById('vacationApplicationForm').method = 'POST';
    if (showWarning == true){
        var agree = confirm("wtf?");
        if (agree){
            document.getElementById('vacationApplicationForm').submit();
        } else {
            return false;
        }
    } else {
        document.getElementById('vacationApplicationForm').submit();
    }
}

And when I click cancel in confirm dialog the form is still being submitted. 当我在“确认”对话框中单击“取消”时,仍在提交表单。 What am I doing wrong here ? 我在这里做错了什么?

Change: 更改:

<form class="form-horizontal" method="POST" onSubmit="submitFormButton()">

To: 至:

<form class="form-horizontal" method="POST" onSubmit="return submitFormButton()">

When using html attributes, you need to use return keyword! 使用html属性时,您需要使用return关键字!

You're not returning the value in the onsubmit attribute. 您没有在onsubmit属性中返回该值。 It should be: 它应该是:

onsubmit="return submitFormButton();"

调用函数时,您需要返回函数的返回值:

onSubmit="return submitFormButton()"

您只需要return返回值即可停止提交:

<form class="form-horizontal" method="POST" onSubmit="return submitFormButton()">

Update form function like below; 更新表单功能,如下所示;

<form class="form-horizontal" method="POST" onSubmit="submitFormButton();return false;">

Here is a working demo: http://jsfiddle.net/cubuzoa/WnRa2/ 这是一个工作示例: http : //jsfiddle.net/cubuzoa/WnRa2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM