使用jQuery和AJAX进行表单验证时,未正确设置变量

时间:2022-10-13 15:23:33

I'm validating a form using Javascript and jQuery.

我正在使用Javascript和jQuery验证表单。

At the end of the validation function I just AJAX to check an entry against a SQL database to see if it already exists. I have that working fine but I don't seem to be able to update the Javascript variable to indicate whether the check returned that the entry exists or not. Below is a pared down version of the validation function.

在验证函数的末尾,我只使用AJAX对SQL数据库中的条目进行检查,看看它是否已经存在。我可以正常工作,但是我似乎不能更新Javascript变量来指示检查是否返回条目存在。下面是验证功能的简化版本。

At the end when I do the AJAX call if the customer exists, I try to set the isformcomplete variable to false but that doesn't work, it's almost as if it's not reading that line and without that set properly, the form gets erroneously submitted.

在完成AJAX调用时(如果客户存在),我尝试将isformcomplete变量设置为false,但这不起作用,就好像它没有读到这一行,如果没有正确设置,表单就会被错误提交。

What am I missing here? The AJAX call is working properly. Thanks.

我错过了什么?AJAX调用正在正常工作。谢谢。

<form method="post" action="../../../scripts/create-customer.asp" id="create-customer" onsubmit="return create_customer();">

function create_customer()
{
    var isformcomplete = true;
    message = "The following tasks need to be completed before continuing:\n\n";
    if ( $("#customerno").val() == "" )
    {
        message += "* Select the customer's country and enter its unique identifier\n";
        isformcomplete = false;
    }
    if ( $("#product").val() == "" )
    {
        message += "* Select a product\n";
        isformcomplete = false;
    }

    if (isformcomplete == false)
        { alert(message); }
    else
    {
        $("#alertsDynamic").slideUp();
        $.post("/global-marketing/scripts/check-new-customer.asp",{ fromsubmit: true, customer: $("#customerno").val()+"|"+$("#product").val()}, 
        function(data, status)
            {
                if ( data == "true" )
                {
                    $("#error-message").html($("#customerno").val()+" "+$("#product").val()+" already exists");
                    $("#alertsDynamic").slideDown();
                    isformcomplete = false;
                }
            }
        );
    }
    return isformcomplete;

1 个解决方案

#1


3  

you should always return false in your function

在函数中应该总是返回false

function create_customer()
{
  message = "The following tasks need to be completed before continuing:\n\n";
  if ( $("#customerno").val() == "" )
  {
    message += "* Select the customer's country and enter its unique 
               identifier\n";
    isformcomplete = false;
}
if ( $("#product").val() == "" )
{
    message += "* Select a product\n";
    isformcomplete = false;
}

if (isformcomplete == false)
    { alert(message); }
else
{
    $("#alertsDynamic").slideUp();
    $.post("/global-marketing/scripts/check-new-customer.asp",{ fromsubmit: true, customer: $("#customerno").val()+"|"+$("#product").val()}, 
    function(data, status)
        {
            if ( data == "true" )
            {
                $("#error-message").html($("#customerno").val()+" "+$("#product").val()+" already exists");
                $("#alertsDynamic").slideDown();
                // Here you need to send the form in case every thing is fine

            }else{
                $("form").submit();
            }
        }
    );
}
return false;

#1


3  

you should always return false in your function

在函数中应该总是返回false

function create_customer()
{
  message = "The following tasks need to be completed before continuing:\n\n";
  if ( $("#customerno").val() == "" )
  {
    message += "* Select the customer's country and enter its unique 
               identifier\n";
    isformcomplete = false;
}
if ( $("#product").val() == "" )
{
    message += "* Select a product\n";
    isformcomplete = false;
}

if (isformcomplete == false)
    { alert(message); }
else
{
    $("#alertsDynamic").slideUp();
    $.post("/global-marketing/scripts/check-new-customer.asp",{ fromsubmit: true, customer: $("#customerno").val()+"|"+$("#product").val()}, 
    function(data, status)
        {
            if ( data == "true" )
            {
                $("#error-message").html($("#customerno").val()+" "+$("#product").val()+" already exists");
                $("#alertsDynamic").slideDown();
                // Here you need to send the form in case every thing is fine

            }else{
                $("form").submit();
            }
        }
    );
}
return false;