0

我有一个时事通讯注册表单,我很高兴。我在下面有这个 jQuery 脚本:

$(document).ready(function(){  
$('#newsletter-signup').submit(function(){  

    //setup variables  
    var form = $(this),  
    formData = form.serialize(),  
    formUrl = form.attr('action'),  
    formMethod = form.attr('method'),   
    responseMsg = $('#signup-response');  

    //show response message - waiting  
    responseMsg.hide()  
               .addClass('response-waiting')  
               .text('Please Wait...')  
               .fadeIn(200);  

    //send data to server for validation  
    $.ajax({  
        url: formUrl,  
        type: formMethod,  
        data: formData,  
        success:function(data){  

            //setup variables  
            var responseData = jQuery.parseJSON(data),   
                klass = '';  

            //response conditional  
            switch(responseData.status){  
                case 'error':  
                    klass = 'response-error';  
                break;  
                case 'success':  
                    klass = 'response-success';  
                break;    
            }  

            //show reponse message  
            responseMsg.fadeOut(200,function(){  
                $(this).removeClass('response-waiting')  
                       .addClass(klass)  
                       .text(responseData.message)  
                       .fadeIn(200,function(){  
                           //set timeout to hide response message  
                           setTimeout(function(){  
                               responseMsg.fadeOut(200,function(){  
                                   $(this).removeClass(klass);  
                               });  
                           },3000);  
                        });  
             });  
          }  
    });  

    //prevent form from submitting  
    return false;  
});  
});

如果您有兴趣,这里是我的 PHP 的一个片段(删除了数据库凭据):

<?php 
if(isset($_GET['action'])&& $_GET['action'] == "signup"){  
mysql_connect(***REMOVEDforprivacy*******);  
mysql_select_db(***REMOVEDforprivacy*******);  

//sanitize data  
$email = mysql_real_escape_string($_POST['signup-email']);  

$to ='***REMOVEDforprivacy*******';
$subject = '***REMOVEDforprivacy*******';
$body = "The email address ". $email. " has been added to the email database.";
$headers = 'From: ***REMOVEDforprivacy*******' . "\r\n" .
            'Reply-To:  ***REMOVEDforprivacy*******' . "\r\n" .
            'X-Mailer:  PHP/' . phpversion();

//validate email address - check if input was empty  
if(empty($email)){  
    $status = "error";  
    $message = "You did not enter an email address!";  
}  
else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ //validate email address - check if is a valid email address  
    $status = "error";  
    $message = "You have entered an invalid email address!";  
}  
else {  
   $existingSignup = mysql_query("SELECT * FROM signups WHERE email_address='$email'");  
   if(mysql_num_rows($existingSignup) < 1){  



       $insertSignup = mysql_query("INSERT INTO signups (email_address) VALUES ('$email')");  
       if($insertSignup){  
           $status = "success";  
           $message = "You have been signed up!";
            mail($to, $subject, $body, $headers);
       }  
       else {  
           $status = "error";  
           $message = "Oops, There has been a technical error!";  
       }  
    }  
    else {  
        $status = "error";  
        $message = "Looks like you have already registered this email address with us.  Thank you for your support!";  
    }  
}  

//return json response  
$data = array(  
    'status' => $status,  
    'message' => $message  
);  

echo json_encode($data);  

exit;  
}  


?>

一切顺利,直到这里:

//show reponse message  
            responseMsg.fadeOut(200,function(){  
                $(this).removeClass('response-waiting')  
                       .addClass(klass)  
                       .text(responseData.message)  
                       .fadeIn(200,function(){  
                           //set timeout to hide response message  
                           setTimeout(function(){  
                               responseMsg.fadeOut(200,function(){  
                                   $(this).removeClass(klass);  
                               });  
                           },3000);  
                        });  
             });  

我不明白这里有什么问题。但是,当我尝试删除大部分语法并仅使用时,语法似乎很好responseMsg.fadeOut(200);responseMsg 不会褪色(因此您可以忘记它删除类并添加新类)。我已经用 Firebug 对此进行了检查,并且 POST 响应确实显示了正确的错误并且正在返回成功消息......(并且确实检查了我的数据库,添加了条目并发送了一封警报电子邮件)......所以除了花哨的 schnazzy JQuery 效果外,一切都在工作。我短暂地认为我与页面上的另一个元素发生了 JQuery 冲突,所以我删除了它,并且没有任何变化。我尝试使用 $.noconflict()、$.noconflict(true),并用 JQuery 替换所有 $ 实例(但是,页面上的其他项目无论如何都不冲突)。我尝试删除添加“响应等待”类的调用,并在数据的 JSON 解析后添加响应消息类,但这不起作用。这个很棒的教程,所以我不知道为什么它不起作用。有人有线索吗?

更新

这里的 FWIW 是一个JSFiddle,尽管如果没有 PHP DB 的东西它绝对无法工作......不知道我该如何解决这个问题,伙计们......

更新 让它工作,将我的 PHP 代码放在我的 html 标题上方的愚蠢错误......一旦我被允许,将作为答案发布。

4

2 回答 2

1

我的意思是你应该减少代码来复制你的问题。一般来说,你会找出你的问题。这很好用(通过单击触发事件来模拟表单提交):

http://jsfiddle.net/k8nhR/3/

$(document).ready(function(){  
    $('#signup-button').click(function(){  

        responseMsg = $("#signup-response");
                 //show response message - waiting  
         responseMsg.hide()  
                    .addClass('response-waiting')  
                    .text('Please Wait...')  
                    .fadeIn(200);  

        var responseData =  { status : "success", message: "success message"};
                var klass = '';

                //response conditional  
                switch(responseData.status){  
                    case 'error':  
                        klass = 'response-error';  
                    break;  
                    case 'success':  
                        klass = 'response-success';  
                    break;  
                }  

                //show reponse message  
                responseMsg.fadeOut(200,function(){  
                   $(this).removeClass('response-waiting')  
                          .addClass(klass)  
                          .text(responseData.message)  
                          .fadeIn(200,function(){  
                              //set timeout to hide response message  
                              setTimeout(function(){  
                                  responseMsg.fadeOut(200,function(){  
                                      $(this).removeClass(klass);  

                                  });  
                               },3000)  
                           });  
                });   
      });  
    });  

​</p>

于 2012-07-23T20:56:23.483 回答
0

您需要在成功函数之前声明 Klass 变量,以便回调函数可以检索它,而不是使用 setTimeout,只需使用 .delay()

于 2012-07-23T20:45:23.727 回答