bootstrap验证器重置表单不起作用

时间:2014-12-05 11:25:32

标签: jquery twitter-bootstrap validation

我正在尝试使用bootstrap验证器重置表单

当我点击重置时,它会删除输入值,但是当我点击提交时,即使输入为空,它仍然提交,当它应该显示错误

这是js

    $('#shortener').bootstrapValidator({
        container: '#sstatus',
        fields: {
            url: {
                validators: {
                    notEmpty: {
                        message: 'Enter a url!'
                    },
                    uri: {
                        message: 'URL is not a valid url, include http://'
                    }
                }
            }
        }
    }).on('success.form.bv', function(e) {
            // Prevent form submission
            e.preventDefault();

            console.log('in');

            // Get the form instance
            var $form = $(e.target);

            // Get the BootstrapValidator instance
            var bv = $form.data('bootstrapValidator');

            // Use Ajax to submit form data
            $.post('ajax/new_url.php', $form.serialize(), function(result) {

                console.log('works'+result.message);

                $('#shortener').fadeOut('fast', function(){
                    $form.bootstrapValidator('resetForm', true); 
                });

                $(document).on('click', '#shorten_again', function(e){
                    e.preventDefault();
                });

            }, 'json');
    });

2 个答案:

答案 0 :(得分:1)

$('form')。bootstrapValidator('resetForm',true);

答案 1 :(得分:-1)

您需要在提交,清除和重置时解决方法:

    .on('init.field.bv', function(e, data) {
        // data.bv      --> The BootstrapValidator instance
        // data.field   --> The field name
        // data.element --> The field element

        var $parent = data.element.parents('.form-group'),
            $icon   = $parent.find('.form-control-feedback[data-bv-icon-for="' + data.field + '"]');

        // From v0.5.3, you can retrieve the icon element by
        // $icon = data.element.data('bv.icon');

        $icon.on('click.clearing', function() {
            // Check if the field is valid or not via the icon class
            if ($icon.hasClass('glyphicon-remove')) {
                // Clear the field
                data.bv.resetField(data.element);
            }
        });
    })

参考:BootstrapValidator Clearing Field Example