form_script.blade.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <div id="ajaxForm" style="z-index:999;display:none;"></div>
  2. <script type="text/javascript">
  3. $(function () {
  4. var t = new Vue({
  5. el:'#ajaxForm',
  6. methods: {
  7. ajaxSuccess: function(message,redirect) {
  8. this.$notify({
  9. title: '成功',
  10. message: message,
  11. type: 'success',
  12. duration: 1500,
  13. onClose: function(){
  14. if( ! redirect ){
  15. var tip = tips_layer.pop();
  16. refresh();
  17. layer.close(tip);
  18. layer.closeAll();
  19. }else{
  20. parent.location.href = redirect;
  21. }
  22. }
  23. });
  24. },
  25. ajaxError: function (message) {
  26. this.$notify.error({
  27. title: '失败',
  28. message: message,
  29. duration: 2500
  30. });
  31. },
  32. ajaxWaring: function () {
  33. this.$notify({
  34. title: '警告',
  35. message: '请求失败',
  36. type: 'warning',
  37. duration: 3500
  38. });
  39. },
  40. ajaxInfo: function () {
  41. this.$notify.info({
  42. title: '提示',
  43. message: '请编辑后再保存!!!',
  44. duration: 2000
  45. });
  46. },
  47. ajaxValidatorFail: function (message, th) {
  48. this.$notify({
  49. title: '提示',
  50. message: message,
  51. type: 'info',
  52. duration: 2500,
  53. onClose: function(){
  54. $($(th).find('button')[0]).prop('disabled',false);
  55. $($(th).find('button')[0]).css('cursor','pointer');
  56. }
  57. });
  58. }
  59. }
  60. });
  61. $('form').submit(function (event) {
  62. if( window.form && !window.form.valid() ){
  63. return false;
  64. }
  65. // 验证数据是否修改
  66. if( window.formDatum && window.formDatum == $('form').serialize() ){
  67. //> 提示信息
  68. t.ajaxInfo();
  69. return false;
  70. }
  71. var evt = event || window.event;
  72. var th = $(evt.target);
  73. // 前端防止重复提交数据
  74. $($(th).find('button')[0]).prop('disabled',true);
  75. $($(th).find('button')[0]).css('cursor','not-allowed');
  76. $.ajax({
  77. url: th.prop('action'),
  78. type: th.prop('method'),
  79. data: th.serialize(),
  80. dataType: 'json',
  81. success: function (res) {
  82. if( res.status == 'success' ){
  83. t.ajaxSuccess(res.message,res.redirect);
  84. }else{
  85. // 处理回调错误信息
  86. if( res.error ){
  87. // 数据验证失败提醒
  88. t.ajaxValidatorFail(res.message,th);
  89. return false;
  90. }else{
  91. // 数据保存失败
  92. t.ajaxError(res.message);
  93. // 失败取消提交按钮
  94. $($(th).find('button')[0]).prop('disabled',false);
  95. $($(th).find('button')[0]).css('cursor','pointer');
  96. }
  97. }
  98. },
  99. error: function () {
  100. // 失败取消提交按钮
  101. $($(th).find('button')[0]).prop('disabled',false);
  102. $($(th).find('button')[0]).css('cursor','pointer');
  103. t.ajaxWaring();
  104. }
  105. });
  106. return false;
  107. });
  108. });
  109. </script>