2016-03-04 65 views
0

我想每次我更新成功時調用的函數。更新正在進行中,我唯一關心的是每次成功更新時的警報彈出窗口。如何調用javascript中的函數?

$.post(
     { 
       url: 'update_question.php', 
       data: 
       { 
        id: id, 
        new_question: newText, 
       }, 

       success: function() 
       { 
        that.replaceWith("<section>"+newText+"</section>"); 

        if(text != newText) 
        { 
         popup(); 
        } 

       } 
     }); 

     var popup = function() { 
     $(document).ready (function(){ 

     $("#myWish").click(function showAlert() { 
      $("#success-alert").alert(); 
      $("#success-alert").fadeTo(2000, 500).slideUp(500, function(){ 
      $("#success-alert").alert('close'); 
      }); 
     }); 
    }); 
    }; 
+1

'$(文件)。 ready(function(){..'在網頁的生命週期中僅被調用一次... – Rayon

+0

@RayonDabre我該怎麼做?remove? –

+0

@VanAdrianCabrera你可以像[this](https:// jsfiddle .net/v6tjtrtd/1 /)。 – RRK

回答

0
var popup = function() { 
     $("#success-alert").alert(); 
     $("#success-alert").fadeTo(2000, 500).slideUp(500, function(){ 
     $("#success-alert").alert('close'); 
    }); 
    }; 

在第一次更新,彈出顯示,但它不會對月2日更新

0

$(document).ready jQuery函數等待,直到DOM被加載到瀏覽器中執行包含在其中的javascript代碼之前的功能範圍{}。

所以從你的代碼中刪除$(document).ready

還要注意,單頁應用程序只需要列出$(document).ready一次,所有的監聽器事件您設置的它的內部定義。

所以你應該至少在一個地方列出它,然後在它的主體中定義所有的初始事件監聽器。

0

表明我認爲這將解決您的問題

$.post(
    { 
     url: 'update_question.php', 
     data: 
     { 
      id: id, 
      new_question: newText, 
     }, 
     success: function() 
     { 
      that.replaceWith("<section>"+newText+"</section>"); 
      if(text != newText){ 
       popup(); 
      } 
     } 
    }); 

function popup() { 
    $("#success-alert").alert(); 
    $("#success-alert").fadeTo(2000, 500).slideUp(500, function(){ 
     $("#success-alert").alert('close'); 
    }); 
}; 
+0

在第一次更新時,彈出窗口顯示,但未在第二次更新中顯示 –