這是在所有這些的最佳解決方案,讓您以當控制你想顯示什麼文字 -
只需添加beforeSend
和complete
方法在你的jQuery請求中。下面是一個例子
// contact form handling using AJAX
$('#contact-form').submit(function() {
var contactForm = $('#contact-form'),
btnSubmit = $('.btn-submit');
$.ajax({
type: contactForm.attr('method'),
url: contactForm.attr('action'),
data: contactForm.serialize(),
beforeSend: function() { // while sending the request the button is disabled and it shows loading spinner
btnSubmit.prop('disabled', true);
btnSubmit.html('<i class="fa fa-spinner fa-spin fa-fw btn-icon fa-2x"></i>');
},
complete: function() { // after the request is sent, the spinner is gone and now it's back to normal. One can submit again
btnSubmit.prop('disabled', false);
btnSubmit.html('<i class="fa fa-paper-plane fa-lg btn-icon" aria-hidden="true"></i> Submit');
},
success: function(response) {
if (response == 'success') {
alert("Hi, your query has been sent. I will be back to you soon. Thank you :)");
} else {
var erroMsg = response.split('<br/>').join('\n');
alert(erroMsg);
}
}
});
return false;
});
正如你可以看到beforeSend
使您能夠控制發送請求時,該怎麼辦,complete
讓你在何時請求發送成功會發生什麼控制。
希望它有幫助。謝謝。
$('#buttonid')。val('micimacko');或在你的情況下 $(this).val('rofibeka'); – Jauzsika