使用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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM