使用bootstrap-wizard onNext进行twitter bootstrap验证

时间:2014-10-31 10:17:32

标签: jquery twitter-bootstrap twitter jqbootstrapvalidation twitter-bootstrap-wizard

我在多个twitter-wizard标签中加载了一个表单,我需要验证当前标签,然后使用BootstrapValidator移动到下一个标签

'onNext': function(tab, nav, index) {
     var $validator=$('#staffForm').bootstrapValidator(options).bootstrapValidator('validate');
    var $valid = $("#staffForm").valid(); <-- this is line 177, the error line
    if(!$valid) {
      $validator.focusInvalid();
         return false;
    }
 }

我收到此错误

Uncaught TypeError: undefined is not a function VM7348:177
$.bootstrapWizard.onNext VM7348:177

我做错了什么?

3 个答案:

答案 0 :(得分:1)

试试这个

'onNext': function(tab, navigation, index) {
                                    var $validator = $('#YOURFORM').data('bootstrapValidator').validate();
                                    return $validator.isValid();
                            }

答案 1 :(得分:0)

经过多次搜索,我发现了另一种方法

$('#wizard').bootstrapWizard({onNext: function(tab, navigation, index) {
        $valid = true;
        $newUser = $('#newUser').data('bootstrapValidator'); //the validator
        $wizard = $('#wizard').data('bootstrapWizard'); //the wizard

        $tab = $('#wizard').find('.tab-content').children().eq($wizard.currentIndex())
        $tab.find('input:text, input:password, input:file, select, textarea, input:not([type=hidden])')
            .each(function() {
                if ($newUser.options.fields[$(this).attr('name')]) {
                    $newUser.validateField($(this).attr('name'));
                    if ($(this).closest('.form-group').hasClass('has-error')){
                        $valid = false;
                        }
                    }
            });
        return $valid ;

    }

我希望这对任何人都有用

答案 2 :(得分:0)

我希望这也能奏效![bootstrap 4]

$('#rootwizard').bootstrapWizard({
        onNext: function (tab, navigation, index) {
            var form = $($(tab).data("targetForm"));
            if (form) {
                form.addClass('was-validated');
                if (form[0].checkValidity() === false) {
                    event.preventDefault();
                    event.stopPropagation();
                    return false;
                }
            }
        },
        onTabClick: function(activeTab, navigation, currentIndex, nextIndex) {
            if (nextIndex <= currentIndex) {
                return;
              }
              var form = $($(activeTab).data("targetForm"));
            if (form) {
                form.addClass('was-validated');
                if (form[0].checkValidity() === false) {
                    event.preventDefault();
                    event.stopPropagation();
                    return false;
                }
            }
            if (nextIndex > currentIndex+1){
               return false;
              }
        }
    });