I have a jQuery code like this:
$("#erase").click(function(){
$("#erase").attr("disabled", "disabled"); // Prevent double-clicking
$(this).html("Erasing...");
$.post("erase.php", {target: $("#target").val(), criteria: $("#criteria").val()}, function(data){
$(this).html(data); // Change the button's caption to whatever erase.php echoes.
setTimeout(function(){
window.location.href = "index.php";
}, 3000);
});
The target and criteria are both HTML input tags and the button is declared with the button tag.
I was expecting these would happen when user clicks the button:
- Button will be greyed out and its text will say 'Erasing...'
erase.phpwill be called via AJAX.erase.phpwill delete a row from a database, and takes several seconds to complete.- When the AJAX call is completed, the button text will be the output of
erase.php. It will be either "Success" or "Failure" - After 3 seconds, user will be brought back to home page.
But in my case, step 3 fails. The button gets stuck in 'Erasing....' (The request does complete, however.)
Side note: I can't really think of a better title for this question. If you can come up with one for me. Please let me know in the comments. I'd appreciate it.
alert(data);before$(this).html(data);console.log(this);