有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
