3

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

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 似乎并没有阻止表单提交。有没有人有任何想法,因为我很难过!

谢谢!

4

5 回答 5

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;"

于 2012-08-26T00:03:44.500 回答
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 请求是异步的,并且内部的代码不会及时运行以停止表单进程。

于 2012-08-26T00:08:19.943 回答
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();
            }
        }
    });
}

一切正常!

感谢大家的投入:) }

于 2012-08-27T11:13:34.777 回答
0

尝试在语句event.preventDefault()之前使用。return

于 2012-08-25T23:53:55.627 回答
0

而不是使用

onsubmit="return FORMVALIDATE_add_rota_entry();"

尝试使用

onclick="return FORMVALIDATE_add_rota_entry();"

您可能需要return true;在函数末尾添加。

于 2013-10-09T15:01:23.153 回答