bootstrapValidator的自動認可方式,用於ajax驗證是需要點擊兩次提交才能成功提交

來源:互聯網
上載者:User

標籤:pre   弊端   對象   note   attr   default   icons   on()   sep   

 1 $(‘#defaultForm‘).bootstrapValidator({ 2                     message: ‘This value is not valid‘, 3                     feedbackIcons: { 4                         valid: ‘glyphicon glyphicon-ok‘, 5                         invalid: ‘glyphicon glyphicon-remove‘, 6                         validating: ‘glyphicon glyphicon-refresh‘ 7                     }, 8                     fields: { 9                         acGroupName: {10                             validators: {11                                 notEmpty: {12                                     message: ‘使用者組名不可為空‘13                                 },14                                 stringLength: {15                                     max: 30,16                                     message: ‘使用者組名過長‘17                                 },18                                 remote: {19                                     url: "<%=basePath%>/accountGroup/existAccountRepeat",20                                     message: ‘使用者組名已存在‘,21                                     data:{id:function(){22                                         return $("#defaultForm input[name=‘id‘]").val();23                                     }}24                                 }25                        26                             }27                         },28                         acGroupContent: {29                             validators:{30                                 notEmpty: {31                                     message: ‘使用者組資訊不可為空‘32                                 },33                             }34                         },35                         strategyGroupId: {36                             validators:{37                                 notEmpty: {38                                     message: ‘策略組不可為空‘39                                 },40                             }41                         },42                     }43                 }).on(‘success.form.bv‘, function(e) {44                     if(againSubmit){45                         return ;46                     }47                     againSubmit = true;                    48                     // 終止重複提交49                     e.preventDefault();50                     // 得到form表單對象51                     var $form = $(e.target);52                     // 獲得bootstrap驗證對象53                     var bv = $form.data(‘bootstrapValidator‘);54                     // 使用Ajax提交form表單資料55                     $.post($form.attr(‘action‘), $form.serialize(), function(result) {56                         if(result==‘1‘){57                             window.top.layer.alert(‘儲存成功!‘, {icon: 6, title:"提示"},function(index){58                             window.top.layer.close(index);59                                   closeLayer(); 60                               });61                         }else{62                             parent.layer.alert("儲存失敗!", {title:"提示"});63                             againSubmit = false;64                         }65                     }, ‘json‘);66                 });

可以看到remote那裡有一個ajax驗證重名,效果是bootstrapValidator沒有等ajax拿到傳回值就直接拿了一個預設值false走人了。所以自動認可方式弊端很大  經過修改,如下:即可成功

 1             $(‘#defaultForm‘).bootstrapValidator({ 2                     message: ‘This value is not valid‘, 3                     feedbackIcons: { 4                         valid: ‘glyphicon glyphicon-ok‘, 5                         invalid: ‘glyphicon glyphicon-remove‘, 6                         validating: ‘glyphicon glyphicon-refresh‘ 7                     }, 8                     fields: { 9                         acGroupName: {10                             validators: {11                                 notEmpty: {12                                     message: ‘使用者組名不可為空‘13                                 },14                                 stringLength: {15                                     max: 30,16                                     message: ‘使用者組名過長‘17                                 },18                                 remote: {19                                     url: "<%=basePath%>/accountGroup/existAccountRepeat",20                                     message: ‘使用者組名已存在‘,21                                     data:{id:function(){22                                         return $("#defaultForm input[name=‘id‘]").val();23                                     }}24                                 }25                        26                             }27                         },28                         acGroupContent: {29                             validators:{30                                 notEmpty: {31                                     message: ‘使用者組資訊不可為空‘32                                 },33                             }34                         },35                         strategyGroupId: {36                             validators:{37                                 notEmpty: {38                                     message: ‘策略組不可為空‘39                                 },40                             }41                         },42                     }43                 });44                   $(‘#defaultForm‘).submit(function(){45                       var flag = $(‘#defaultForm‘).data("bootstrapValidator").isValid();46                     setTimeout(function(){47                         var flag2 = $(‘#defaultForm‘).data("bootstrapValidator").isValid();48                         if(flag2){49                             var $form = $(‘#defaultForm‘);50                             // 使用Ajax提交form表單資料51                             $.post($form.attr(‘action‘), $form.serialize(), function(result) {52                                 if(result==‘1‘){53                                     window.top.layer.alert(‘儲存成功!‘, {icon: 6, title:"提示"},function(index){54                                     window.top.layer.close(index);55                                           closeLayer(); 56                                       });57                                 }else{58                                     parent.layer.alert("儲存失敗!", {title:"提示"});59                                     againSubmit = false;60                                 }61                             }, ‘json‘);62                         }63                           }, 500);64                     });

可以看到,我不用自動認可  我通過一個延時處理等待ajax傳回值即可。

bootstrapValidator的自動認可方式,用於ajax驗證是需要點擊兩次提交才能成功提交

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.