4

我已经设法让 jQuery 验证插件与 RequireJs 一起使用。我现在正在尝试设置 jquery 验证默认参数,以便它不会验证包装在具有 data-prefix 属性的标记中的字段(这是在 Page1.js 文件中完成的)。但是,当您单击提交按钮时,它会尝试验证这两个字段。

这是代码(原始问题链接到更复杂的演示):

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
    <meta charset="utf-8" />
    <script src="http://ajax.cdnjs.com/ajax/libs/require.js/2.1.5/require.js"></script>
    <script>
        require.config({
            paths: {
                'jquery': 'http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.3',
                'jquery.validate': 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate',
                'jquery.validate.unobtrusive': 'http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive'
            },
            shim: {
                'jquery.validate': ['jquery'],
                'jquery.validate.unobtrusive': ['jquery', 'jquery.validate']
            }
        });

        require(['jquery', 'jquery.validate', 'jquery.validate.unobtrusive'], function($) {
            $.validator.setDefaults({
                ignore: '[data-js-val-prefix] *'
            });
        });
    </script>
</head>
<body>
    <form>
        <div class="validation-summary-valid" data-valmsg-summary="true"><span>Please correct the errors and try again.</span>
            <ul><li style="display:none"></li></ul>
        </div>
        <fieldset>
            <legend>Include</legend>
            <input name="Include" type="text" required />
        </fieldset>
        <fieldset data-js-val-prefix="Exclude">
            <legend>Exclude</legend>
            <input name="Exclude" type="text" required />
        </fieldset>
        <p><button type="submit">Submit</button></p>
    </form>
</body>
</html>

我没有使用库的缩小版本,因为它在调试时可能会有所帮助。

我会很感激帮助。谢谢

编辑:

如果您将脚本块更改为以下任一项,则可以正常工作。

没有RequireJs:

<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.3.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.js"></script>
<script>
    $.validator.setDefaults({
        ignore: '[data-js-val-prefix] *'
    });
</script>

没有不显眼的 JavaScript:

<script>
    require.config({
        paths: {
            'jquery': 'http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.3',
            'jquery.validate': 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate'
        },
        shim: {
            'jquery.validate': ['jquery']
        }
    });

    require(['jquery', 'jquery.validate'], function($) {
        $.validator.setDefaults({
            ignore: '[data-js-val-prefix] *'
        });

        $(function() {
            $('form').validate();
        });
    });
</script>

这向我表明,不显眼的验证库和 RequireJs 一起存在问题。我确定我在做一些微不足道的错误。

4

1 回答 1

7

I've answered my own question with the help of @Sparky. The issue happens because the jquery.validate.unobtrusive file wires up the validate method, if you then say $.validator.setDefaults in the callback function, it would have already wired up the validation and it will be ignored. If you change the require script block to the following:

require(['jquery', 'jquery.validate'], function($) {
    $.validator.setDefaults({
        ignore: '[data-js-val-prefix] *'
    });

    require(['jquery.validate.unobtrusive']);
});

All is dandy. I hope this helps.

于 2013-06-14T14:49:44.847 回答