123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <div id="ajaxForm" style="z-index:500;display:none;"></div>
- <script type="text/javascript">
- $(function () {
- var t = new Vue({
- el:'#ajaxForm',
- methods: {
- ajaxSuccess: function(message,redirect) {
- this.$notify({
- title: '成功',
- message: message,
- type: 'success',
- duration: 1500,
- onClose: function(){
- if( ! redirect ){
- var tip = tips_layer.pop();
- refresh();
- layer.close(tip);
- layer.closeAll();
- }else{
- parent.location.href = redirect;
- }
- }
- });
- },
- ajaxError: function (message) {
- this.$notify.error({
- title: '失败',
- message: message,
- duration: 2500
- });
- },
- ajaxWaring: function () {
- this.$notify({
- title: '警告',
- message: '请求失败',
- type: 'warning',
- duration: 3500
- });
- },
- ajaxInfo: function () {
- this.$notify.info({
- title: '提示',
- message: '请编辑后再保存!!!',
- duration: 2000
- });
- }
- }
- });
- $('form').submit(function (event) {
- if( window.form && !window.form.valid() ){
- return false;
- }
- // 是否开启验证机制 and 验证数据是否修改
- if( window.formDatum && window.formDatum == $('form').serialize() ){
- //> 提示信息
- t.ajaxInfo();
- return false;
- }
- var evt = event || window.event;
- var th = $(evt.target);
- // 前端防止重复提交数据 注释按钮
- $($(th).find('button')[0]).prop('disabled',true);
- $($(th).find('button')[0]).css('cursor','not-allowed');
- $.ajax({
- url: th.prop('action'),
- type: th.prop('method'),
- data: th.serialize(),
- dataType: 'json',
- success: function (res) {
- if( res.status == 'success' ){
- t.ajaxSuccess(res.message,res.redirect);
- }else{
- // 处理回调错误信息
- if( res.error ){
- window.tipsC = [];
- // 数据后台验证失败
- for( key in res.message ){
- window.tipsC[key] = layer.tips(res.message[key].join(' | '),'form input[name="'+key+'"]',{tips: [3,'#FF4949'],time:4000,tipesMore:true,
- end:function () {
- // 失败取消提交按钮
- $($(th).find('button')[0]).prop('disabled',false);
- $($(th).find('button')[0]).css('cursor','pointer');
- layer.close(window.tipsC[key]);
- return false;
- }});
- }
- }else{
- // 数据保存失败
- t.ajaxError(res.message);
- // 失败取消提交按钮
- $($(th).find('button')[0]).prop('disabled',false);
- $($(th).find('button')[0]).css('cursor','pointer');
- }
- }
- },
- error: function () {
- // 失败取消提交按钮
- $($(th).find('button')[0]).prop('disabled',false);
- $($(th).find('button')[0]).css('cursor','pointer');
- t.ajaxWaring();
- }
- });
- return false;
- });
- });
- </script>
|