2016-06-09 62 views
0

這裏淡入淡出效果我的腳本:影響了我的輸出

<div id ="success-message"> 

</div> 


<script> 
$('form.subscribe').on('submit', function() { 
var that = $(this), 
url = that.attr('action'), 
method = that.attr('method'), 
data ={}; 
that.find('[name]').each(function(index, value) { 
    var that = $(this), 
    name = that.attr('name'), 
    value = that.val(); 

data[name] = value; 
}); 

$.ajax({ 
url: url, 
type:method, 
data: data, 
success: function(response){ 


$('#success-message').html(response) 

} 

}); 

return false; 


}); 

setTimeout(function() { 
    $('#success-message').fadeOut('slow'); 
}, 2000); // <-- time in milliseconds 
    </script> 

我有一個Ajax功能,讓我提供一個錯誤信息,或者根據你的投入是窗體上的成功消息。

我得到來自這個PHP腳本中的消息:

<?php 
    header("Refresh:7; url=contact.php"); 
    $email = $_POST['subscribefield']; 

    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
     echo "Dit emailadres klopt niet"; 
     die(); 
    } 

    $to = "[email protected]"; 
    $subject = "Abonee voor de nieuwsbrief"; 
    $body = "$email \n Heeft zich aangemeld voor de nieuwsbrief"; 

    mail($to, $subject, $body); 
    echo "U heeft zich zojuist aangemeld voor de vandenberg nieuwsbrief"; 
?> 

我現在輸出2個回波的從一個div PHP文件稱爲成功的消息,到目前爲止,一切順利。但我想使消息dissapear經過一段時間,所以我添加了這個功能:

setTimeout(function() { 
    $('#success-message').fadeOut('slow'); 
}, 2000); // <-- time in milliseconds 

,但現在它只是閉上眼睛,例如錯誤表明我的消息,現在它只能說明我一個消息時,形式是沒有正確填充。當它的正確填充我什麼也得不到。

回答

1

將回應寫入div後,如果已經隱藏了它,請確保再次顯示div。

喜歡的東西:

$('#success-message').html(response).show(); 
1

廣場成功函數內部的setTimeout()代碼塊。當你運行代碼時,它會立即將#success-message css改爲display:none

所以,你可以解決它像這樣:

$.ajax({ 
url: url, 
type:method, 
data: data, 
success: function(response){ 

$('#success-message').html(response); 

setTimeout(function() { 
    $('#success-message').fadeOut('slow'); 
}, 2000); 

} 

}); 
相關問題