使用bootstrap validator异步提交,出现提交两次问题!
有bug的代码如下:
$("#report").bootstrapValidator({ message: 'This value is not valid', feedbackIcons: { valid: 'glyphicon glyphicon-ok', validating: 'glyphicon glyphicon-refresh' }, fields: { reportOne: { message: '请输入红包金额', validators: { notEmpty: { message: "红包金额不能为空" }, regexp: { regexp: /^[0-9]+(.[0-9]{1,2})?$/, message: '请输入正确的金额' } } } } }).on('success.form.bv', function (e) {
//按照其他攻略上加了这段代码依然无效果 e.preventDefault(); console.log('test'); });
现象:第一次进入页面,点击保存(表单验证是通过的);会打印两个test;
该问题官方文档中给出了几种解决方案:
文档地址(有墙):http://formvalidation.io/examples/form-submit-twice/
参考文档,我通过给submit按钮绑定事件来完成提交;
$("#report").bootstrapValidator({ message: 'This value is not valid', feedbackIcons: { valid: 'glyphicon glyphicon-ok', validating: 'glyphicon glyphicon-refresh' },
fields: { reportOne: { message: '请输入红包金额', validators: { notEmpty: { message: "红包金额不能为空" }, regexp: { regexp: /^[0-9]+(.[0-9]{1,2})?$/, message: '请输入正确的金额' } } } }
}).on('success.form.bv', function (e) { e.preventDefault(); }); $("#reportButton").click(function (e) { console.log('test'); });
点击提交后只打印一次test