返回false不停止表单提交

时间:2012-08-25 23:50:25

标签: javascript jquery forms

我很确定这不应该那么难。我有一个表单在onsubmit上运行以下函数:

function FORMVALIDATE_add_rota_entry() {
    var rota_date = $("#rota_date").val();
    var rota_id = $("#rota_id").val();
    var am_pm = $("#am_pm").val();
    if(rota_date == "")
    {
        alert("Please enter a date.");
        return false;
    }
    if(rota_id == "error")
    {
        alert("Please select a rota from the list.");
        return false;
    }
    // check if that rota has already been entered for that date and am/pm
    $.ajax({
        async:false,
        type:"POST",
        url:"/modules/rotas/check_rota_entry_existence",
        data:{rota_date:rota_date, rota_id:rota_id, am_pm:am_pm},
        success: function(result) {
            if(result != "0")
            {
                alert("This rota has already been added to this date, "+am_pm+".");
                return false;
            }
        }
    });
}

通过以下方式在表单标记中调用:

onsubmit="return FORMVALIDATE_add_rota_entry();"

它在前两个工作正常,rota_date和rota_id,当它们应该发出警报时它会停止提交表单但是当它进入ajax调用时,它会很好,返回正确的结果,应该提醒,但返回false似乎并没有阻止提交表单。有没有人有任何想法,因为我很难过!

谢谢!

5 个答案:

答案 0 :(得分:6)

你正确地做错了。你这样做的方式,你应该让函数总是返回false:

function FORMVALIDATE_add_rota_entry() {
    var rota_date = $("#rota_date").val();
    var rota_id = $("#rota_id").val();
    var am_pm = $("#am_pm").val();
    if(rota_date == "")
    {
        alert("Please enter a date.");
        return false;
    }
    if(rota_id == "error")
    {
        alert("Please select a rota from the list.");
        return false;
    }
    // check if that rota has already been entered for that date and am/pm
    $.ajax({
        async:false,
        type:"POST",
        url:"/modules/rotas/check_rota_entry_existence",
        data:{rota_date:rota_date, rota_id:rota_id, am_pm:am_pm},
        success: function(result) {
            if(result != "0")
            {
                alert("This rota has already been added to this date, "+am_pm+".");
                return false;//this is called when the server responds....which in your case isnt happening in the first place

            }
        }
    });

    return false;//this should be at the end so 'false' is always returned to 'onsubmit'
}

if语句中的return false;用于防止ajax代码运行的唯一目的。但是在功能的最后也应该有一个。 这应该是一个更容易解决的问题:

onsubmit="FORMVALIDATE_add_rota_entry();return false;"

答案 1 :(得分:1)

卸下:

onsubmit="return FORMVALIDATE_add_rota_entry();"

在您的函数下面添加(其中my-form是表单的ID):

$('#my-form').submit(FORMVALIDATE_add_rota_entry);

变化:

function FORMVALIDATE_add_rota_entry() {

到:

function FORMVALIDATE_add_rota_entry(event) {

在你的职能改变中:

return false;

event.preventDefault();

这并不总是暂停表单提交,因为ajax请求是异步的,并且内部代码不会及时运行以停止表单进程。

答案 2 :(得分:1)

好的,所以在尝试了上述解决方案并没有取得绝对的成功后,我自己来了。我完全抛弃了表单元素并发送了三个值,如下所示:

 onclick="FORMVALIDATE_add_rota_entry($('#rota_date').val(), $('#rota_id').val(), $('#am_pm').val());"

然后,功能变为如下:

function FORMVALIDATE_add_rota_entry(rota_date, rota_id, am_pm) {
    // check if that rota has already been entered for that date and am/pm
    $.ajax({
        type:"POST",
        url:"/modules/rotas/check_rota_entry_existence",
        data:{rota_date:rota_date, rota_id:rota_id, am_pm:am_pm},
        success: function(result) {
            if(result != "0")
            {
                alert("This rota has already been added to this date, "+am_pm+".");
            }
            else if(rota_date == "")
            {
                alert("Please enter a date.");
            }
            else if(rota_id == "error")
            {
                alert("Please select a rota from the list.");
            }
            else
            {
                $.post('/modules/rotas/add_rota_entry2', {rota_date:rota_date, rota_id:rota_id, am_pm:am_pm});
                parent.$.fn.colorbox.close();
            }
        }
    });
}

一切都按预期工作!

感谢大家输入:) }

答案 3 :(得分:0)

尝试在event.preventDefault()声明之前使用return

答案 4 :(得分:0)

而不是使用

onsubmit="return FORMVALIDATE_add_rota_entry();"

尝试使用

onclick="return FORMVALIDATE_add_rota_entry();"

您可能需要在功能结束时添加return true;