javascript - 使用验证引擎时成功功能不起作用

标签 javascript jquery ajax

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

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

有什么好的解决方案吗?

最佳答案

问题是这样的:

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

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

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

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

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

关于javascript - 使用验证引擎时成功功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12609206/

相关文章:

javascript - 在浏览器中使用 Nunjucks,我如何只请求一次模板来迭代使用不同的值?

javascript - 我将如何为一个元素添加一个顶部偏移量,当它到达窗口顶部时,该元素会粘住?

javascript - 我应该使用什么脚本来实现平滑的 anchor 滚动?我试过的都不起作用

javascript - setState 不设置值

javascript call() 函数此上下文

javascript - 如何清除间隔并重新设置?

javascript - 在服务器上的 session 管理的所有请求中传递参数

javascript - Ajax成功功能好像不起作用

php - 下拉菜单中未选择任何选项

javascript - jQuery ajax post请求不发送数据