【问题标题】:bootstrap-combobox (by Daniel Farrel) and jquery-validaiton-pluginbootstrap-combobox (by Daniel Farrell) 和 jquery-validation-plugin
【发布时间】:2016-11-06 16:02:45
【问题描述】:

有没有人让 jquery.validate 插件与 Daniel Farrel 创建的 bootstrap-combobox 一起工作?

jquery.validate 插件可以很好地与其余的引导元素一起使用,但无法使其与引导组合框一起使用。

这里是html:

<div class="row">
                <div class="col-sm-4">
                    <label for="categoryParent" class="control-label">Category</label>
                </div>
                <div class="col-sm-8">
                    <select id="categoryParent" name="categoryParent" class="combobox form-control">
                        <option value="" selected="selected">Select the category</option>
<option>...</option>

                    </select>
                </div>
            </div>

和js:

$.validator.setDefaults({
        highlight: function(element) {
            $(element).closest('.row').addClass('has-error');
        },
        unhighlight: function(element) {
            $(element).closest('.row').removeClass('has-error');
        },
        errorElement: 'span',
        errorClass: 'help-block',
        errorPlacement: function(error, element) {
            error.insertAfter(element);
        }
    });

    $("#addProductForm").validate({
        rules: {
            categoryParent: {required: true}
        },
        messages: {                
            categoryParent : "Category required"

        },
        invalidHandler: function() {
            $("#cc-alert").css("display","block");
        },
        submitHandler: function() {

    //
    // more code
    //


            return false;
        }

【问题讨论】:

    标签: jquery twitter-bootstrap jquery-validate


    【解决方案1】:

    Bootstrap ComboBox 隐藏了原来的select,并用更吸引人的东西取而代之。但是,jQuery Validate 插件只能验证原始的select 元素,但也会因为它被隐藏而忽略它。

    您只需使用the ignore option 将其告知validate hidden elements

    ignore: []  // ignore nothing, validate everything
    

    在您的代码中:

    $("#addProductForm").validate({
        ignore: [],
        rules: { ....
    

    或者:

    $.validator.setDefaults({
        ignore: [],
        highlight: function(element) { ....
    

    【讨论】:

    • 第一次尝试就成功了!非常感谢,你拯救了我的一天。
    • 知道如何删除“errorClass” :) 吗?一旦字段(组合框)被验证,它就会保留在那里。而它与其他字段正确隐藏。
    • @user1611183,使用highlightunhighlight 选项添加/删除类。您必须为您的 ComboBox 元素自定义这些功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多