0

我无法使脚本工作,当我运行它时它总是返回错误

$(document).ready(function()
{
    $("#form1").validationEngine({
        ajaxSubmit: true,
        ajaxSubmitFile: "note/note.php",
        success: false
    });
    if (success == "true")
    {
        function()
        {
            window.alert("Report Sent!");
            //write a confirmation to the user
            document.getElementById("update").innerHTML="Report Sent!";
            setTimeout(function()
            {
                document.getElementById("update").innerHTML="";
            },3000);
        }
    }
});

什么可能是一个好的解决方案?

4

2 回答 2

2

这是问题所在:

$(document).ready(function()
{
    $("#form1").validationEngine({
        ajaxSubmit: true,
        ajaxSubmitFile: "note/note.php",
        success: false
    });

您将一个对象文字传递给该validationEngine方法,该success属性应该是一个函数,其中包含您希望在验证成功时看到执行的代码,而是将其设置为 false,然后像这样继续:

if (success == "true")
{
     function()
     {

您不仅使用实际上是对象属性的变量(因此您正在创建一个全局变量,即EVIL),您正在声明该函数,但没有调用它。我注意到您正在检查 的值success,期望它是一个字符串。您的 php 脚本的返回值将作为参数传递给成功回调函数。我也冒昧地更改了函数以反映这一点:

$(document).ready(function()
{
    $("#form1").validationEngine({
        ajaxSubmit: true,
        ajaxSubmitFile: "note/note.php",
        success: function(response)//response is what the php script returns
        {
            //the function from your if branch goes here
        }
    });

你应该没事。完整的代码应该类似于:

$(document).ready(function()
{//notice the curly braces around what you're passing to validationEngine
    $("#form1").validationEngine({
        ajaxSubmit: true,
        ajaxSubmitFile: "note/note.php",
        success: function(response)
        {
            if (response !== 'true')
            {
                if (window.console)
                {
                    console.log(response);
                    alert('check your console to see the response');
                }
                else
                {
                    alert(response);
                }
                return;
            }
            alert("Report Sent!");//no need for window here
            document.getElementById("update").innerHTML="Report Sent!";
            setTimeout(function()
            {
                document.getElementById("update").innerHTML="";
            },3000);
        }
    });
    //As I said, you're passing an object, so you could write the code above like so:
    var validationArgs = {ajaxSubmit:true,ajaxSubmitFile:'note/note.php'};
    validationArgs.success = function()
    {
        //your function
    };
    $('#form1').validationEngine(validationArgs);
});
于 2012-09-26T19:50:52.773 回答
0

您需要为此使用回调函数。

$(document).ready(function() {
    var update = $("#update");
    // Callback 
    var onSuccess = function() {
        alert("Report Sent!");

        //write a confirmation to the user           
        update.html("Report Sent!");

        setTimeout(function(){
             update.html("");
        },3000);
    };

    $("#form1").validationEngine({
        ajaxSubmit: true,
        ajaxSubmitFile: "note/note.php",
        success: onSuccess
   });
});
于 2012-09-26T19:57:20.820 回答