我有一个包含带有input属性的type="image"的表单。无论是成功还是失败,我每次都在改变形象。
但它并不总是起作用,我不明白为什么。有没有任何可能的方法来消除setTimeout(function())或替代的方式来实现这个动画,因为这可能导致问题?
<p class="text-center" style="color:brown" id="input_result">Send</p>
<form data-ajax="false" method="post" name="login_form" role="form">
<div class="form-group">
<input type="image" src="images/default.png" class="img-responsive center-block" name="submit" class="submitBtn" id="input_img">
</div>
</form>脚本
<script>
$('form').bind('submit', function() {
$.ajax({
type: 'POST',
url: 'includes/sendmail.php',
data: $(this).serialize(),
cache: false,
dataType: 'text',
success: function(data, status, xhttp) {
$("#input_img").slideUp(2000).slideDown(1000);
setTimeout(function() {
// Success animation
$('#input_img').attr("src", "images/success.png");
jQuery("input[type='image']").prop("disabled", true);
$('selector').click(false);
$("#input_result").text("Sent!");
$("#input_result").css("color", "green");
}, 1999);
this.submit();
},
error: function(jqXHR, textStatus, errorThrown) {
$("#input_img").slideUp(2000).slideDown(1000);
setTimeout(function() {
// Fail animation
$('#input_img').attr("src", "images/fail.png");
jQuery("input[type='image']").prop("disabled", true);
$('selector').click(false);
$("#input_result").text("Failed to send!");
$("#input_result").css("color", "red");
}, 1999);
},
});
return false;
})
</script>发布于 2016-04-06 10:15:44
当您使用this.submit();提交表单时,页面将提交到服务器并再次重新加载,因此您将丢失成功动画。因为要在函数中提交值,所以不需要提交表单。
发布于 2016-04-06 10:15:36
与$("#input_img").slideUp(2000).slideDown(1000);不同,您可以使用slideUp的回调来运行您的函数,然后在函数的末尾调用.slideDown() Like:$("#input_img").slideUp(2000, function(){ $('#input_img').attr("src", "images/fail.png"); jQuery("input[type='image']").prop("disabled", true); $('selector').click(false); $("#input_result").text("Failed to send!"); $("#input_result").css("color", "red"); $("#input_img").slideDown(1000); }
https://stackoverflow.com/questions/36447779
复制相似问题