即使onsubmit函数返回false,也会提交表单

时间:2014-03-12 14:37:06

标签: javascript forms

所以我的表格看起来像这样:

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

和我的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();
    }
}

当我在确认对话框中单击取消时,表单仍在提交中。我在这里做错了什么?

5 个答案:

答案 0 :(得分:0)

变化:

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

要:

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

使用html属性时,您需要使用return关键字!

答案 1 :(得分:0)

您未在onsubmit属性中返回值。它应该是:

onsubmit="return submitFormButton();"

答案 2 :(得分:0)

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

onSubmit="return submitFormButton()"

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

更新表单功能,如下所示;

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

以下是一个有效的演示: http://jsfiddle.net/cubuzoa/WnRa2/