jQuery Validation插件,IE7"SCRIPT3:未找到成员"

Joe*_*zel 15 javascript jquery jquery-validate internet-explorer-7 compatibility-mode

我有以下内容:

<html>
    <head>
    </head>
    <body>
        <div>
            <form method="post">
                <div id="questions">    
                    <label for="question-6">Name of Course:</label>
                    <input type="text" name="name_of_course[response]" value="" id="question-6" class="required">
                    <label class="control-label" for="reporting-year">Reporting Year: </label>
                    <select name="reporting_year" id="reporting-year">
                        <option value="-1" selected="selected">Select option...</option>
                        <option value="4">2013-2014</option>
                        <option value="1">2012-2013</option>
                        <option value="2">2011-2012</option>
                        <option value="3">2010-2011</option>
                    </select>
                </div>
                <input type="submit" name="submit" value="Save Entry" class="btn">
            </form>
        </div>
        <script src="//code.jquery.com/jquery.js"></script>
        <script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.min.js"></script>
        <script>
            $(function(){
                jQuery.validator.addMethod("notEqual", function(value, element, param) {
                        return this.optional(element) || value !== param;
                        }, "Please select an option");
                $('form').validate({
                    rules:{
                        'reporting_year': {
                            notEqual: "-1"
                        }
                    }
                });
            });
        </script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

每个人最喜欢的浏览器,IE7(IE10 w /兼容性)在控制台中报告以下错误:

SCRIPT3:找不到会员.

jquery.js,第2525行第4个字符

当然IE8及以上版本工作正常,但我的客户端正在使用IE7.

dan*_*ser 24

看起来这是IE10在兼容模式下的一个错误,因为据报道它可以在IE7中运行.但是这里有一些jquery解决方法:http: //bugs.jquery.com/ticket/12577

  • www.browserstack.com对于测试旧版浏览器非常有用 (2认同)

Xar*_*kis 6

我发现导致问题的是jquery.validate.js的第35行

this.attr('novalidate', 'novalidate');
Run Code Online (Sandbox Code Playgroud)

注释掉这一行并解决了问题.您也可以使用(当前浏览器<= ie7)来包装它,以便仅在ie7是broswer时明确地避免使用此行.

更新 要仅为ie7注释掉行,您可以使用以下代码:

var ie = (function () {
        var undef,
    v = 3,
    div = document.createElement('div'),
    all = div.getElementsByTagName('i');
        while (
    div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
    all[0]
);
        return v > 4 ? v : undef;
    } ());
Run Code Online (Sandbox Code Playgroud)

然后:

    if (!ie || ie > 7) {
      this.attr('novalidate', 'novalidate');
    }
Run Code Online (Sandbox Code Playgroud)