简体   繁体   English

Internet Explorer IE 8中的jQuery问题

[英]jQuery Issue in Internet Explorer IE 8

I set a global variable, $category_list, which is an array of category names. 我设置了一个全局变量$ category_list,它是一个类别名称数组。

In the footer I have: 在页脚我有:

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

I have an external .js file with the following code: 我有一个带有以下代码的外部.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 );
    }
});

An error occurs in IE8 so the dropdown doesn't work: wordlist[...] is null or not an object IE8中发生错误,因此下拉列表不起作用: wordlist[...] is null or not an object

Not sure how to get round this, the dropdown list of category names works fine in IE 9, Firefox and Chrome but it doesn't on IE8, affecting around 50% of IE users. 不确定如何绕过这一点,类别名称的下拉列表在IE 9,Firefox和Chrome中运行良好但在IE8上没有,影响了大约50%的IE用户。 I'm using the Codeigniter framework. 我正在使用Codeigniter框架。 I've tried loading the footer script before the external file but no joy. 我已经尝试在外部文件之前加载页脚脚本但没有快乐。

Much love for any help <3 非常喜欢任何帮助<3

IE8 doesn't like trailing , in array literal definitions. 在数组文字定义中, IE8不喜欢尾随。 You'll have to refactor your code to not leave one. 您将不得不重构代码而不留下代码。

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

or 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM