[英]Putting PHP echo into a button's innerHTML doesn't work
我有這樣的jQuery代碼:
$("#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);
});
target
和criteria
都是HTML input
標簽,並且按鈕是使用button
標簽聲明的。 我期望這些會在用戶單擊按鈕時發生:
erase.php
將通過AJAX調用。 erase.php
將從數據庫中刪除一行,並且需要幾秒鍾才能完成。 erase.php
的輸出。 這將是“成功”或“失敗” 但就我而言,步驟3失敗。 該按鈕卡在“正在擦除......”中(但是請求確實完成了。)
旁注:我真的想不出這個問題的更好的標題。 如果您能為我提出一個建議。 請在評論中讓我知道。 我會很感激的。
$ .post中的$(this)不是您以前擁有的。 試試這個
$("#erase").click(function(){
$("#erase").attr("disabled", "disabled"); // Prevent double-clicking
$(this).html("Erasing...");
$this = $(this);
$.post("erase.php", {target: $("#target").val(),
criteria: $("#criteria").val()}, function(data){
$this.html(data);
setTimeout(function(){
window.location.href = "index.php";
}, 3000);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.