JQuery自动完成将不允许从下拉IE中进行选择

JQuery Autocomplete will not allow selection from dropdown IE

本文关键字:IE 选择 行选 不允许 JQuery      更新时间:2023-09-26

我的代码适用于I.E.9、Chrome和Safari,但许多人在使用旧版本的Internet Explorer和当前版本的Firefox时遇到了问题。它按预期显示下拉选择,但不允许用户单击选择。这是代码:

<script type="text/javascript" src="js/jquery-1.6.2.js"></script>
<script type="text/javascript" src="js/jquery.ui.core.js"></script>
<script type="text/javascript" src="js/jquery.ui.datepicker.js"></script>
<script type="text/javascript" src="js/jquery.ui.widget.js"></script>
<script type="text/javascript" src="js/jquery.ui.position.js"></script>
<script type="text/javascript" src="js/jquery.ui.autocomplete.js"></script>
<link type="text/css" href="css/jquery-ui-1.8.17.custom.css" rel="Stylesheet" />
<style>
.ui-autocomplete-loading { background: white url('images/ui-anim_basic_16x16.gif') right center no-repeat; }
</style>
<script>
$(function() {
    function log( message ) {
        $( "<div/>" ).text( message ).prependTo( "#inputString" );
        $( "#inputString" ).scrollTop( 0 );
        showCar(message);
    }
    $( "#inputString" ).autocomplete({
        source: "ajax/search.php",
        minLength: 1,
        select: function( event, ui ) {
            log( ui.item ?
                "Selected: " + ui.item.value :
                "Nothing selected, input was " + this.value );
        }
    });

});
</script>

使用开发工具,它会在控制台中显示以下错误:SCRIPT65535:对方法或属性访问的意外调用。jquery-1.6.2.js,第5609行字符5

这表明:

prepend: function() {
    return this.domManip(arguments, true, function( elem ) {
        if ( this.nodeType === 1 ) {
            this.insertBefore( elem, this.firstChild );
        }
    });
},

编辑现在它抛出一个错误:SCRIPT438:对象不支持属性或方法"prepend"jquery-1.6.2.js,第5975行,字符4

即:

jQuery.each({
appendTo: "append",
prependTo: "prepend",
insertBefore: "before",
insertAfter: "after",
replaceAll: "replaceWith"
}, function( name, original ) {
jQuery.fn[ name ] = function( selector ) {
    var ret = [],
        insert = jQuery( selector ),
        parent = this.length === 1 && this[0].parentNode;
    if ( parent && parent.nodeType === 11 && parent.childNodes.length === 1 && insert.length === 1 ) {
        insert[ original ]( this[0] );
        return this;
    } else {
        for ( var i = 0, l = insert.length; i < l; i++ ) {
            var elems = (i > 0 ? this.clone(true) : this).get();
            jQuery( insert[i] )[ original ]( elems );
            ret = ret.concat( elems );
        }
        return this.pushStack( ret, name, insert.selector );
    }
};
});

5974行是本节的顶行:

if ( parent && parent.nodeType === 11 && parent.childNodes.length === 1 && insert.length === 1 ) {
        insert[ original ]( this[0] );
        return this;
    }

如果您的项目使用编译,您可能会考虑在入口点的顶部安装并添加文件

import 'core-js'

目前,core-js-polyfill库是使跨浏览器支持的最简单方法