回答
$.ajax({
type="get",
url="someurl.html",
dataType:"html",
beforeSend(jqXHR, settings)
{
//append spinner image to some html element
},
success:function(data)
{
//manipulate data
},
Complete:(jqXHR, textStatus)
{
//Remove spinner from the page
}
});
假設你有500個項目(文件/不管)來處理的進展過程中,...
你不得不單獨處理每一個項目。
然後遍歷所有項目和:
- 做一個$就呼叫到您的ItemController的「句柄」的行動,處理一個項目
- 回報,如果一個JSON結果代碼
- OK,移動jQuery的進展事情了一步
- 如果失敗了,要麼做下一個,或取消整個過程(根據您的要求)
偉大的鏈接。 THanks Tassadaque – 2011-04-15 00:21:46
我結束了使用這些偉大的資源:
- http://contextllc.com/dev_tools/jQuery/showLoading/latest/jquery.showLoading.example.html
- http://ajaxload.info/
容易和簡單。
不是很好的資源,但有用。第二個鏈接生成一個動畫GIF。 – RaoulRubin 2012-05-23 17:07:23
所以我發現了這個問題,我正在尋找,但我不喜歡官方答案的概念......爲什麼?很好地使用AJAX提交表單很不錯,但是你失去了ASP MVC的力量,以及他將@model
返回給View的概念,並且通過使用AJAX,您還必須更改大量代碼才能返回AJAX/JSON響應(更改返回ActionResult JsonResult,等等......)。我不想失去的ActionResult
的輸出與return View(model)
,因爲它涉及非常方便(尤其是與jQuery驗證掛在模型)......於是我想出了使用替代以下:
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
...
<div class="form-group">
<button id="saveBtn" type="submit" class="btn btn-primary">Save</button>
@Html.ActionLink("Back to List", "Index")
</div>
}
@section Scripts {
<!-- jQuery ASP Validation -->
@Scripts.Render("~/Scripts/jqueryval")
<script type="text/javascript">
$(document).ready(function() {
// clear error summary from inside our global variable (defined in _layout view)
window.App.errorSummary = {};
// Attach click handler to the submit button
// from there, attach a spinner but only if no error summary are showing
$('#saveBtn').click(function() {
// has to be inside a timer else it would be triggered too late
setTimeout(function() {
if (window.App.errorSummary.numberOfInvalids < 1) {
// your spinner here... in my case spinner.js
}
}, 50);
});
});
</script>
}
和你還必須擴展jQuery的驗證對象,但最好將其擴展您的_Layout
視圖中,並從那裏我的代碼看起來是這樣的:
window.App = {
baseUrlArea: '@((ViewContext.RouteData.Values["area"] == null) ? "" : ViewContext.RouteData.Values["area"])',
baseUrl: '@Url.Content("~/")',
language: '@((ViewContext.RouteData.Values["language"] == null) ? "en" : ViewContext.RouteData.Values["language"])',
controller: '@ViewContext.RouteData.Values["controller"]',
action: '@ViewContext.RouteData.Values["action"]',
errorSummary: {}
};
$(document).ready(function() {
// extend the jquery validator object to keep track of error summary
// save summary inside the global (window.App) variable
if (typeof $.validator !== "undefined") {
$.validator.setDefaults({
showErrors: function (errorMap, errorList) {
window.App.errorSummary.numberOfInvalids = this.numberOfInvalids();
window.App.errorSummary.errorMap = errorMap;
window.App.errorSummary.errorList = errorList;
$("#summary").html("Your form contains "
+ this.numberOfInvalids()
+ " errors, see details below.");
this.defaultShowErrors();
}
});
}
});
你可能想,爲什麼與numberOfInvalids
麻煩?如果不這樣做,點擊保存按鈕時會顯示調整器,儘管我們只希望它在驗證錯誤摘要爲空時顯示出來......有意義嗎?所以,你去了,我們現在通過jQuery驗證重獲ASP/MVC的全部力量!而我們的微調只在需要展示的時候纔會展示。甜!!! :)
非常感謝你 - 真的很甜!不知道爲什麼在一整年之後你還沒有得到任何讚揚,但這是我從一週前開始研究這個問題以來所見到的最好的解決方案,只發現了有缺陷的技術。榮譽給你。 – PIntag 2015-05-21 16:31:12
您可以使用ajaxStart/ajaxStop。 jQuery 1。8例如:
$(document).ready(function() {
$("#btnShowSpinner").click(ajaxToExecute); // wire up click event for the button
(function() { $("#loading").hide(); })(); // hide the spinner initially
$(document).ajaxStart(function() { $("#loading").show(); }); // show spinner when ajax request executes
$(document).ajaxStop(function() { $("#loading").hide(); });// show spinner when ajax request completes
});
在您的網頁標記添加包含微調,以向用戶顯示一個div:
<input id="btnShowSpinner" type="button" value="Show Spinner" />
<div id="loading"><img src="spinner.gif" /></div>
Ajax調用:
function ajaxToExecute() {
$.ajax({
cache: false,
type: "POST",
url: myURL
data: { "id": id},
success: function (data) {
updateDiv.html(data);
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Request failed.\nError: ' + thrownError);
}
});
}
這種設計也將允許重用微調頁面上的任何其他Ajax請求。
- 1. 蓮花的XPages擴展庫數量微調,防止提交
- 2. 如何驗證提交的微調
- 3. MVC表單提交回調
- 4. 微調提交表單時提交表格
- 5. 添加微調器來輸入jquery提交
- 6. jQuery的 - 在提交
- 7. 提交的JQuery在Chrome
- 8. jQuery UI的微調
- 9. jQuery和擴展[提交不工作]
- 10. 顯示使用jQuery提交響應的進度條ajax提交
- 11. jQuery的形式提交 - 從醒目的結果提交調用
- 12. 編程提交微軟的AJAX形式
- 13. 在asp.net中的微調框mvc
- 14. 在MVC 4中提交的部分視圖和模型jQuery表單提交0
- 15. Android微調提示?
- 16. ASP.Net MVC通過JQuery提交Ajax表單
- 17. asp.net mvc 2.0 jquery表單提交
- 18. MVC 4 unobtrusive jQuery驗證之前提交
- 19. 表單提交使用jquery和mvc
- 20. spring mvc jquery花式框:表單提交
- 21. 防止多個表單提交MVC Jquery
- 22. ASP.Net MVC提交調查問題數據
- 23. 進度微調器
- 24. 如何在android中提交動態微調數據表單?
- 25. 擴展微調項目
- 26. 通過jQuery提交表單提交JSON
- 27. 提交表單不jQuery的AJAX調用
- 28. 提交在JQuery中
- 29. 正在提交jquery
- 30. 交互微調與EditText
謝謝穆罕默德,我會嘗試實施你的建議,看看它是如何去。 – 2011-04-15 00:20:38