javascript - Chosen.js 并验证 jquery

标签 javascript jquery validation jquery-chosen

我正在使用 validate.jquery.js:工作正常。 但是当我添加 chosen.js 时,对选择下拉列表的验证不再起作用。

这是我正在使用的 JS http://pastebin.com/S9AaxdEN

这是我选择的表格:

<select name="category" id="category" placeholder="" class="{validate:{required:true}}">
<option value=""><?php echo lang('category_choice'); ?></option>
<option value="vtt">VTT</option>
<option value="autre">Autre type de v&eacute;lo</option>
</select>

不知道为什么 chosen.js 禁用验证,知道吗?

最佳答案

您可以通过将不采用属性“ignore”的类“chzn-done”添加到“validate.settings”来解决此问题:

var settings = $.data($('#myform')[0], 'validator').settings;
settings.ignore += ':not(.chzn-done)';

example

HTML:

<form method="post" id="form1" action="">
    <fieldset>
        <legend>Login Form</legend>
        <div>
            <label>datos</label>
            <select name="data-select" title="data-select is required" class="chzn-select {required:true}" style="width:150px;">
                <option></option>
                <option value="1">uno</option>
                <option value="2">dos</option>
            </select>
        </div>
        <div>
            <label for="phone">Phone</label>
            <input id="phone" name="phone" class="some styles {required:true,number:true, rangelength:[2,8]}" />
        </div>
        <div>
            <label for="email">Email</label>
            <input id="email" name="email" class="{required:true,email:true}">
        </div>

        <div class="error"></div>
        <div>
            <input type="submit" value="enviar datos"/>
        </div>
    </fieldset>
</form>

JS:

$(function() {

    var $form = $("#form1");

    $(".chzn-select").chosen({no_results_text: "No results matched"});
    $form.validate({
        errorLabelContainer: $("#form1 div.error"),
        wrapper: 'div',
    });

    var settings = $.data($form[0], 'validator').settings;
    settings.ignore += ':not(.chzn-done)';

    $('form').each(function(i, el){

        var settings = $.data(this, 'validator').settings;
        settings.ignore += ':not(.chzn-done)';

    });

});

关于javascript - Chosen.js 并验证 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10387553/

相关文章:

javascript - jQuery 无法获取 attr 值

python - 如何验证下采样是否符合预期

java - 在 Tapestry Web 应用程序中处理域异常的最佳方法是什么?

javascript - 在jqgrid中实现删除和编辑操作

Javascript - 提升优先级

jquery - 我在选择之前有一张图片,当我选择它时应该将其更改为另一张图片

c# - MVC3 和 Ajax : Refresh list to hide a record which was deleted from list by a button click

node.js - 验证 Mongoose 中的子文档

javascript - jQuery 更改表单的响应文本

javascript - 元素返回 NaN 作为宽度