2014-11-04 39 views
2

我正在使用Bootstrap驗證器,我遇到的問題是我希望在所有值都有效後啓用提交按鈕,但無法這樣做。在Bootstrap驗證器中啓用提交按鈕

$(document).ready(function() { 
    $('#ans_frm').bootstrapValidator({ 
     feedbackIcons: { 
      valid: 'glyphicon glyphicon-ok', 
      invalid: 'glyphicon glyphicon-remove', 
      validating: 'glyphicon glyphicon-refresh' 
     }, 
     submitButtons: 'button[type="submit"]', 
     fields: { 
      ans: { 
       group: '.col-md-8', 
       validators: { 

        stringLength: { 
         min: 3, 
         max: 100, 
         message: 'The answer must be more than 2 and less than 100 characters long' 
        }, 
        notEmpty: { 
         message: 'The answer must not be empty' 
        } 
       } 

      } 
     } 
    }).on('status.field.bv', function(e, data) { 

       disableSubmitButtons(false); 
      } 
     }); 
}); 

回答

3

你需要禁用提交按鈕.on('error.field.bv'),並使其回到.on('status.field.bv')

而你應該使用data.bv.disableSubmitButtons()方法!

你可以試試嗎?

$(document).ready(function() { 
    $('#ans_frm').bootstrapValidator({ 
     feedbackIcons: { 
      valid: 'glyphicon glyphicon-ok', 
      invalid: 'glyphicon glyphicon-remove', 
      validating: 'glyphicon glyphicon-refresh' 
     }, 
     submitButtons: 'button[type="submit"]', 
     fields: { 
      ans: { 
       group: '.col-md-8', 
       validators: { 
        stringLength: { 
         min: 3, 
         max: 100, 
         message: 'The answer must be more than 2 and less than 100 characters long' 
        }, 
        notEmpty: { 
         message: 'The answer must not be empty' 
        } 
       } 
      } 
     } 
    }).on('error.field.bv', function(e, data) { 
      data.bv.disableSubmitButtons(true); // disable submit buttons on errors 
     } 
    }).on('status.field.bv', function(e, data) { 
      data.bv.disableSubmitButtons(false); // enable submit buttons on valid 
     } 
    }); 
}); 
+0

它確實值傳送到下一個頁面,但它並不validate.The按鈕啓用所有的時間甚至當值不正確。 – Umerm 2014-11-09 18:26:32

2

使用此

$('#ans_frm').bootstrapValidator({ 
      feedbackIcons: { 
       valid: 'glyphicon glyphicon-ok', 
       invalid: 'glyphicon glyphicon-remove', 
       validating: 'glyphicon glyphicon-refresh' 
      }, 
      live: 'enabled', 
      trigger: null 
     }).on('success.form.bv', function (e) { 
      // Prevent submit form 
      // e.preventDefault(); 
     }) 
     .on('error.form.bv', function() { 

     });