【问题标题】:jQuery - Form Validation with tabs outside of formjQuery - 使用表单外的选项卡进行表单验证
【发布时间】:2015-06-29 12:27:07
【问题描述】:

我正在使用FormValidation plugin for jQuery - 我正在尝试使用bootstrap tabs 创建一个表单向导。到目前为止,这是我的代码:

<form class="center-form" id="installationForm" method="post">
    <!-- Tab panes -->
    <div class="tab-content">
        <! -- #tab1 -->
        <div role="tabpanel" class="tab-pane active" id="details">
            <div class="form-group">
                <label>Title</label>
                <input type="text" class="ad-title form-control" name="title" value="" placeholder="Enter your advertisement title...">
            </div>

        </div>
        <! -- #tab1 end -->
        <! -- #tab2 -->
        <div role="tabpanel" class="tab-pane active" id="audience">
            <div class="form-group">
                <label>Description</label>
                <input type="text" class="ad-title form-control" name="description" value="" placeholder="Enter your advertisement title...">
            </div>
        </div>
        <! -- #tab2 end -->

    </div>
    <ul class="pager wizard">
        <li class="previous"><a href="javascript:void(0)">Previous</a>

        </li>
        <li class="next"><a href="javascript:void(0);">Next</a>

        </li>
    </ul>
</form>

<ul class="nav nav-tabs ad-creation" id="progress">
        <li class="active"> <a href="#details" data-toggle="tab" class="button btn-round">tab1</a></li>
        <li> <a href="#audience" data-toggle="tab" class="button btn-round">tab2</a></li>

还有我的javascript:

$(document).ready(function() {
    $('#installationForm')
        .formValidation({
            framework: 'bootstrap',
            icon: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            // This option will not ignore invisible fields which belong to inactive panels
            excluded: ':disabled',
            fields: {
                name: {
                    validators: {
                        notEmpty: {
                            message: 'The site name is required'
                        }
                    }
                },
                url: {
                    validators: {
                        notEmpty: {
                            message: 'The URL is required'
                        },
                        uri: {
                            message: 'The URL is not valid'
                        }
                    }
                }
            }
        })
        .bootstrapWizard({
            tabClass: 'nav nav-pills',
            onTabClick: function(tab, navigation, index) {
                return validateTab(index);
            },
            onNext: function(tab, navigation, index) {
                var numTabs    = $('#progress').find('.tab-pane').length,
                    isValidTab = validateTab(index - 1);
                if (!isValidTab) {
                    return false;
                }

                if (index === numTabs) {
                    // We are at the last tab

                }

                return true;
            },
            onPrevious: function(tab, navigation, index) {
                return validateTab(index + 1);
            },
            onTabShow: function(tab, navigation, index) {
                // Update the label of Next button when we are at the last tab
                var numTabs = $('#progress').find('.tab-pane').length;
                $('#installationForm')
                    .find('.next')
                        .removeClass('disabled')    // Enable the Next button
                        .find('a')
                        .html(index === numTabs - 1 ? 'Install' : 'Next');

            }
        });

    function validateTab(index) {
        var fv   = $('#installationForm').data('formValidation'), // FormValidation instance
            // The current tab
            $tab = $('#installationForm').find('.tab-pane').eq(index);

        // Validate the container
        fv.validateContainer($tab);

        var isValidStep = fv.isValidContainer($tab);
        if (isValidStep === false || isValidStep === null) {
            // Do not jump to the target tab
            return false;
        }

        return true;
    }
});

现在 - 验证工作 IF 我将 #progress 移动到 #installionForm 表单标签内。

我的问题是如何让它验证选项卡内的表单字段,并且#progress 移出#installationForm

【问题讨论】:

  • 验证仅在项目可见时有效,但您可以关闭此功能,以便验证页面上的所有内容是否隐藏 - 文档解释了如何
  • @DarrenSweeney 过去 3 小时我一直在查看他们的网站 - 仍然找不到任何关于此的信息。你有参考或链接吗?
  • @Kepoly 好主意!虽然没有奏效。不确定#element 是否必须是

标签: javascript jquery html forms twitter-bootstrap


【解决方案1】:

要在隐藏字段时进行验证,我们必须覆盖默认的隐藏时忽略。

这应该适合你:

$('#myform').validate({
    ignore: [],
    // etc etc other validation options
});

验证代码查看代码并基本上创建一个要忽略的字段数组,这会覆盖一个空数组,这意味着验证所有。

【讨论】:

  • 我不太确定如何使用该功能。我没有任何隐藏字段。我只想将我的#progress ul 放在#installationForm div 之外。
猜你喜欢
  • 2023-03-30
  • 2020-12-11
  • 2011-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-12
  • 1970-01-01
相关资源
最近更新 更多