【问题标题】:jQuery Issue in Internet Explorer IE 8Internet Explorer IE 8 中的 jQuery 问题
【发布时间】:2013-03-06 07:28:32
【问题描述】:

我设置了一个全局变量 $category_list,它是一个类别名称数组。

在我的页脚中:

<?php if ( isset($category_list) ): ?>
    <script type="text/javascript">
        var wordlist = [
            <?php foreach ($category_list as $row) { echo '"' . $row->name . '", '; } ?>
        ];
    </script>  
<?php endif ?>

我有一个包含以下代码的外部 .js 文件:

jQuery( ".cats" ).autocomplete({
    source: function(req, responseFn) {
        var matches = new Array();
        var needle = req.term.toLowerCase();

        var len = wordlist.length;
        for(i = 0; i < len; ++i)
        {
            var haystack = wordlist[i].toLowerCase();
            if(haystack.indexOf(needle) == 0 ||
               haystack.indexOf(" " + needle) != -1)
            {
                matches.push(wordlist[i]);
            }
        }
        responseFn( matches );
    }
});

IE8 出现错误,所以下拉菜单不起作用:wordlist[...] is null or not an object

不知道如何解决这个问题,类别名称的下拉列表在 IE 9、Firefox 和 Chrome 中运行良好,但在 IE8 上却不行,影响了大约 50% 的 IE 用户。我正在使用 Codeigniter 框架。我尝试在外部文件之前加载页脚脚本,但没有任何乐趣。

非常感谢任何帮助

【问题讨论】:

    标签: php jquery internet-explorer-8


    【解决方案1】:

    IE8 不喜欢在数组字面量定义中尾随 ,。你必须重构你的代码才能不留下任何东西。

    <?php 
        $rows = array();
        foreach ($category_list as $row) { $rows[] = '"' . $row->name . '"'; } 
        echo implode(',', $rows);
    ?>
    

    var wordlist = <?php 
        $rows = array();
        foreach ($category_list as $row) { $rows[] = '"' . $row->name . '"'; } 
        echo json_encode($rows);
    ?>;
    

    【讨论】:

    • 非常感谢,很好的修复 :-) 只需要 $rows[] 代替 rows[]
    猜你喜欢
    • 2010-10-27
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多