找不到这个jQuery代码中的bug

时间:2011-03-26 20:24:29

标签: php jquery

我有以下代码,按下某个按钮后,应验证电子邮件地址(此功能已经有效),然后应该尝试发布我的表单,如果它返回“错误”,它应该显示错误。我想我正在混淆PHP和JS。

$('#recoverSub').live('click',function() {
        $("#recoverPost").validate({
            rules: {
                recoverField: {
                    email: true
                }
            },
            messages:{
                recoverField: {
                    email: "Not a valid email."
                }
            }
        });
        if($("#recoverPost").valid())
        {
            $.post('php/recoverPost.php', $('#recoverPost').serialize(), function(){
                function(data) {
                    if(data != "Error")
                    {
                        $('#recoverPost').hide();
                        $('#success').show();
                    }
                    else
                    {
                        echo "This email is not in our records.";
                    }
                }
            });
        }
    });

4 个答案:

答案 0 :(得分:5)

else
{
   echo "This email is not in our records.";
}

您可能打算使用alert代替echo

alert("This email is not in our records.");

答案 1 :(得分:2)

实现您想要做的事情的更好方法:

$(function() {
    $('#recoverPost').validate({
        rules: {
            recoverField: {
                 email: true
            }
        },
        messages: {
            recoverField: {
                email: "Not a valid email."
            }
        },
        submitHandler: function(form) {
            // the form was valid => post it with AJAX
            $.post('php/recoverPost.php', $(form).serialize(), function(data) {
                if(data != 'Error') {
                    $('#recoverPost').hide();
                    $('#success').show();
                }
                else {
                    alert("This email is not in our records.");
                }
            });
        }
    });    
});

答案 2 :(得分:1)

 echo "This email is not in our records.";

那是php,就像你想的那样。请改用:

alert("this email is not in our records.";

答案 3 :(得分:1)

$.post中你已经包含了两次函数,第一次没有通过data所以第二次没有机会......

应阅读:

$('#recoverSub').live('click',function() {
        $("#recoverPost").validate({
            rules: {
                recoverField: {
                    email: true
                }
            },
            messages:{
                recoverField: {
                    email: "Not a valid email."
                }
            }
        });
        if($("#recoverPost").valid())
        {
            $.post('php/recoverPost.php', $('#recoverPost').serialize(), function(data){
                    if(data != "Error")
                    {
                        $('#recoverPost').hide();
                        $('#success').show();
                    }
                    else
                    {
                        alert( "This email is not in our records." );
                    }
            });
        }
    });