jquery.Extend.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //重写ajax,post增加loading
  2. (function ($) {
  3. var _ajax = $.ajax;
  4. $.ajaxWithLoading = function (opt) {
  5. var fn = {
  6. error: function (XMLHttpRequest, textStatus, errorThrown) { },
  7. success: function (data, textStatus) { }
  8. };
  9. if (opt.error) {
  10. fn.error = opt.error;
  11. }
  12. if (opt.success) {
  13. fn.success = opt.success;
  14. }
  15. var $loading = $('#loading');
  16. var _opt = $.extend(opt, {
  17. error: function (XMLHttpRequest, textStatus, errorThrown) {
  18. $loading.hide();
  19. fn.error(XMLHttpRequest, textStatus, errorThrown);
  20. },
  21. success: function (data, textStatus) {
  22. $loading.hide();
  23. fn.success(data, textStatus);
  24. }
  25. });
  26. $loading.show();
  27. $.ajax(_opt);
  28. };
  29. $.getWithLoading = function (url, data, callback, type) {
  30. return $.ajaxWithLoading({
  31. url: url,
  32. type: 'get',
  33. dataType: type,
  34. data: data,
  35. success: callback
  36. });
  37. };
  38. $.postWithLoading = function (url, data, callback, type) {
  39. return $.ajaxWithLoading({
  40. url: url,
  41. type: 'post',
  42. dataType: type,
  43. data: data,
  44. success: callback
  45. });
  46. };
  47. })($);
  48. $.fn.extend({
  49. submitWithLoading: function () {
  50. var $loading = $('#loading');
  51. $loading.show();
  52. $(this).submit();
  53. }
  54. });