返回false不停止表单提交

返回false不停止表单提交

本文介绍了返回false不停止表单提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很确定这不应该像现在这样艰难。我有一个运行以下函数的表单:

 函数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(请输入日期。);
返回false;

if(rota_id ==error)
{
alert(请从列表中选择一个旋转);
返回false;

//检查是否已经为该日期输入了rota并且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(这个rota已经添加到这个日期,+ am_pm +。);
return false;
}
}
});
}

它在表单标记中被调用如下:

  onsubmit =return FORMVALIDATE_add_rota_entry(); 

在前两个函数rota_date和rota_id上运行正常,并且它阻止表单提交,但是当它到达ajax调用时,它会很好,返回正确的结果,并在应该时发出警报,但返回false似乎不会阻止表单提交。有没有人有任何想法,因为我很难过!



谢谢!

解决方案

好吧,在尝试上述解决方案并没有绝对的成功之后,我自己来了。

  onclick =FORMVALIDATE_add_rota_entry($('#rota_date')我将表单元素完全抛弃并发送了如下三个值: ).val(),$('#rota_id')。val(),$('#am_pm')。val()); 

然后,函数变成如下:

 函数FORMVALIDATE_add_rota_entry(rota_date,rota_id,am_pm){
//检查是否已经为该日期输入了rota并且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},
成功:函数(结果){
if(result!=0)
{
alert(这个rota已经添加到这个日期,+ am_pm +。);

else if(rota_date ==)
{
alert(请输入日期。);
}
else if( );
}
else
{
$ .POST('/模块/ ROTAS / add_ro ta_entry2',{rota_date:rota_date,rota_id:rota_id,am_pm:am_pm});
parent。$。fn.colorbox.close();
}
}
});

$ / code>

所有工作都是应有的!



感谢每个人的输入:)
}


I'm pretty sure this shouldn't be as hard as it is. I have a form that runs the following function 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;
            }
        }
    });
}

It's called in the form tag by the following:

onsubmit="return FORMVALIDATE_add_rota_entry();"

And it works fine on the first two, rota_date and rota_id, with the alerts coming up when they should and it stopping the form from submitting but when it get's to the ajax call, it makes it fine, returns the right result, alerts when it should, but the return false doesn't seem to be stopping the form from submitting. Does anyone have any ideas because I'm stumped!

Thanks!

解决方案

Ok, so after trying the above solutions with no absolute success I came upon my own. I ditched the form element entirely and sent the three values onclick as below:

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

Then, the function became as follows:

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

All is works as it should!

Thanks for everyones input :)}

这篇关于返回false不停止表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 16:33