2011-05-18 43 views
1

我遇到我的更新按鈕和jquery ajax的問題​​。現在,當我點擊我的更新按鈕時,它將任何更新的數據保存到數據庫中。我的目標是如果更新成功,我想滑動消息。我在看ajax文章,並且使用成功事件看起來好像會起作用,但我不知道如何合併它。我將如何做到這一點?會是這樣的嗎?幫助jquery ajax成功事件

 $(document).ready(function(){ 
     $('#divSuccess').hide(); 

     $('#btnUpdate').click(function() { 
     alert('button click'); 
      $.ajax({ 
        url: "test.aspx", 
        context: document.body, 
        success: function(){ 
        $('#divSuccess').show("slide", { direction: "down" }, 3000); 
        $('#divSuccess').hide("slide", { direction: "down"}, 5000); 
        } 
       }); 
     }); 
    }); 
+0

什麼是服務器語言? – 2011-05-18 14:46:01

+0

爲什麼這麼複雜?爲了您的第一次嘗試,您應該使用一個簡單的警報框。如果它可以工作,創建一個簡單的div,隱藏的默認可見性。在成功事件中,您切換此對象(使用.show()) – reporter 2011-05-18 14:50:44

+0

我目前正在使用vb – bolo 2011-05-18 15:00:31

回答

0

查看這個question關於如何處理成功事件的例子。希望這可以幫助!

0
$("#targetDiv").load("page.php",$("#form").serializeArray(),function (response) 
      { 
       if (response == '0' && response != '') 
       alert('Request not sent to server !\n'); 
       else if(response == '-1') 
       alert('Please write some more !\n'); 
       else 
       { 
       alert("success! "); 
       } 
      } 
     ); 

我回聲版0和-1失敗和其他成功的

+0

typo $(「#form」)=> $(「#form」);) – stecb 2011-05-18 14:48:35

+0

yup,now fixed,thanks :) – Sourav 2011-05-18 14:50:04

0

在jQuery的功能後,就可以執行一些回調函數。

function (data, textStatus) { 
     // data could be xmlDoc, jsonObj, html, text, etc... 
     this; // the options for this ajax request 
     // textStatus can be one of: 
     // "timeout" 
     // "error" 
     // "notmodified" 
     // "success" 
     // "parsererror" 
     // NOTE: Apparently, only "success" is returned when you make 
     // an Ajax call in this way. Other errors silently fail. 
     // See above note about using $.ajax. 
    } 

http://docs.jquery.com/Post

0

至少jQuery的1.5,你已經得到了延期對象和AJAX事件(包括success)新的語法。

var $ajaxcall = $.ajax({ 
    url : 'myurl.svc/somemethod', 
    data : '{ somedata : "sometext" }' 
}); 

$ajaxcall.success(function() { 
    // do something on successful AJAX completion 
}); 

當然你也可以鏈接,並沿着$.ajax().success()或什麼的東西叫一些東西。

只是wrote a blog post on it myself,如果你有興趣閱讀更多。