2012-09-20 7 views
0

簡單
我想用戶後做一些異步服務器端驗證(使用AJAX)點擊「確定」按鈕。如果驗證失敗,我想通過提示顯示錯誤信息而不關閉它。jQuery的即興 - 保持開放的提示,直到異步AJAX效應初探處理

詳細

我使用的臨時版本3.1。我找不到的3.1所以檢查的documentation for version 4.0.1,有兩種方法可以做到這一點的文件 -

返回FALSE或event.preventDefault(),以確保迅速打開

我檢查因爲我使用的是舊版本,事件變量是不喜歡這個裏面提供的功能 -

$.prompt('Example 2',{ buttons: { Ok: true, Cancel: false }, submit: submitCertificationPrompt } }); 

function submitCertificationPrompt(e,v,m,f) 
{ 
//e is not available in case of version 3.0.1 
} 

我不能升級到最新版本,現在,因爲有一些自定義,我們做了版本3.1插件碼。

所以,我留下了使用return false的方式。我查了一下,這個作品 -

$.prompt('Example 2',{ buttons: { Ok: true, Cancel: false }, submit: submitCertificationPrompt } }); 

    function submitCertificationPrompt() 
    { 
     return false; 
    } 

但是,因爲我必須等到AJAX收到響應,我試圖用這樣的JavaScript回調函數,但它不工作像我期望 -

$.prompt('Example 2',{ buttons: { Ok: true, Cancel: false }, submit: function(){ submitCertificationPrompt(function(bool){ return bool; }) } }); 

function submitCertificationPrompt(callback) 
{ 
    //I will do an AJAX call here and the prompt should stay open if the response reads validation error. So, I will callback false in that case 

    callback(false); 
} 

請幫助...

回答

0

嗯,我不太清楚你在做什麼,而是利用一個事實,即$阿賈克斯是實施jQuery deferred interface

$.ajax({ 
    // the opts for the call. 
    // see the documentation. 
}).promise().then(function() 
{ 
    // this gets executed when the ajax call comes back with an OK HTTP code like 200 
    // perform console.log(arguments) here to see what you get. 
}, function() 
{ 
    // this gets executed when the ajax call comes back with an ERROR HTTP code like 500 
    // perform console.log(arguments) here to see what you get. 
}).always(function() 
{ 
    // this gets always executed. 
    // perform console.log(arguments) here to see what you get. 
}); 
+0

謝謝您的回答,但我不確定我是否實際上可以使用延期處理和即興事件處理程序。讓我來描述UI中會發生什麼,用戶點擊某處並出現一個提示(即興表演是一個用於顯示自定義提示的jquery插件),底部有「Ok」和「Cancel」按鈕。我在我的問題中談論這個確定按鈕。然後,如果用戶點擊確定,我想要進行AJAX調用。同時,如果AJAX響應錯誤,我想保持提示並關閉它。 –