layer确认框layer.confirm多次重复提交
点击确定按钮后,会重复提交提交信息,代码如下:
layer.confirm('确认发放激活码?',{icon:3},function(index){ $.ajax({ type: 'POST', url: "<{: U('activation/appointActivation')}>", dataType: 'json', data:{'train_id':train_id,'send_total':send_total,'code_type':code_type}, success: function(data){ if(data.status ==''){ layer.alert(data.msg,{icon: 1},function(){ window.parent.location.reload();//刷新父页面 parent.layer.close(index); }); }else{ layer.alert(data.msg,{icon: 2}); } }, error:function(data) { console.log(data.msg); } }); }) |
在点击确认发放激活码的时候,ajax执行异步操作,并未执行
①给确认按钮加锁解决
var lock = false; //锁 layer.confirm('确认发放激活码?',{icon:3},function(index){ if(!lock){ lock=true; //加锁 $.ajax({ type: 'POST', url: "<{: U('activation/appointActivation')}>", dataType: 'json', data:{'train_id':train_id,'send_total':send_total,'code_type':code_type}, success: function(data){ if(data.status ==''){ layer.alert(data.msg,{icon: 1},function(){ window.parent.location.reload();//刷新父页面 parent.layer.close(index); }); }else{ layer.alert(data.msg,{icon: 2}); } }, error:function(data) { console.log(data.msg); } }); } }) |
②关闭确认框 layer.close(index);
layer.confirm('确认发放激活码?',{icon:3},function(index){ layer.close(index); //关闭弹出框 $.ajax({ type: 'POST', url: "<{: U('activation/appointActivation')}>", dataType: 'json', data:{'train_id':train_id,'send_total':send_total,'code_type':code_type}, success: function(data){ if(data.status ==''){ layer.alert(data.msg,{icon: 1},function(){ window.parent.location.reload();//刷新父页面 parent.layer.close(index); }); }else{ layer.alert(data.msg,{icon: 2}); } }, error:function(data) { console.log(data.msg); } }); }) |
③关闭所有确认框 layer.closeAll(‘dialog’);
layer.confirm('确认发放激活码?',{icon:3},function(index){ layer.closeAll('dialog'); $.ajax({ type: 'POST', url: "<{: U('activation/appointActivation')}>", dataType: 'json', data:{'train_id':train_id,'send_total':send_total,'code_type':code_type}, success: function(data){ if(data.status ==''){ layer.alert(data.msg,{icon: 1},function(){ window.parent.location.reload();//刷新父页面 parent.layer.close(index); }); }else{ layer.alert(data.msg,{icon: 2}); } }, error:function(data) { console.log(data.msg); } }); }) |
推荐第二种使用方式