簡體   English   中英

將PHP回聲放入按鈕的innerHTML中不起作用

[英]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);
        });

targetcriteria都是HTML input標簽,並且按鈕是使用button標簽聲明的。 我期望這些會在用戶單擊按鈕時發生:

  1. 按鈕將顯示為灰色,其文字將顯示“正在刪除...”。
  2. erase.php將通過AJAX調用。 erase.php將從數據庫中刪除一行,並且需要幾秒鍾才能完成。
  3. 當AJAX調用完成時,按鈕文本將是erase.php的輸出。 這將是“成功”或“失敗”
  4. 3秒后,用戶將被帶回到首頁。

但就我而言,步驟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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM