javascript - Jquery不同版本。 Jquery 可搜索自动建议

标签 javascript jquery jquery-ui jquery-autocomplete

我必须在代码中使用两个 jquery API,“jquery-1.8.3.js”和“jquery-ui.js”。 但 jquery 已经有旧版本了。因此,该表格无法正常工作。

所以我在网上搜索解决方案,发现我们可以使用

<script>
    var jq13 = jQuery.noConflict(true);
</script>

但是我必须使用 api,所以我创建了两个变量,但它仍然不起作用。 这是我的代码:-

<script>
(function( $ ) {
    $.widget( "ui.combobox", {
        _create: function() {
            var input,
                that = this,
                select = this.element.hide(),
                selected = select.children( ":selected" ),
                value = selected.val() ? selected.text() : "",
                wrapper = this.wrapper = $( "<span>" )
                    .addClass( "ui-combobox" )
                    .insertAfter( select );

            function removeIfInvalid(element) {
                var value = $( element ).val(),
                    matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( value ) + "$", "i" ),
                    valid = false;
                select.children( "option" ).each(function() {
                    if ( $( this ).text().match( matcher ) ) {
                        this.selected = valid = true;
                        return false;
                    }
                });
                if ( !valid ) {
                    // remove invalid value, as it didn't match anything
                    $( element )
                        .val( "" )
                        .attr( "title", value + " didn't match any item" )
                        .tooltip( "open" );
                    select.val( "" );
                    setTimeout(function() {
                        input.tooltip( "close" ).attr( "title", "" );
                    }, 2500 );
                    input.data( "autocomplete" ).term = "";
                    return false;
                }
            }

            input = $( "<input>" )
                .appendTo( wrapper )
                .val( value )
                .attr( "title", "" )
                .addClass( "ui-combobox-input" )
                .autocomplete({
                    delay: 0,
                    minLength: 0,
                    source: function( request, response ) {
                        var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
                        response( select.children( "option" ).map(function() {
                            var text = $( this ).text();
                            if ( this.value && ( !request.term || matcher.test(text) ) )
                                return {
                                    label: text.replace(
                                        new RegExp(
                                            "(?![^&;]+;)(?!<[^<>]*)(" +
                                            $.ui.autocomplete.escapeRegex(request.term) +
                                            ")(?![^<>]*>)(?![^&;]+;)", "gi"
                                        ), "<strong>$1</strong>" ),
                                    value: text,
                                    option: this
                                };
                        }) );
                    },
                    select: function( event, ui ) {
                        ui.item.option.selected = true;
                        that._trigger( "selected", event, {
                            item: ui.item.option
                        });
                    },
                    change: function( event, ui ) {
                        if ( !ui.item )
                            return removeIfInvalid( this );
                    }
                })
                .addClass( "ui-widget ui-widget-content ui-corner-left" );

            input.data( "autocomplete" )._renderItem = function( ul, item ) {
                return $( "<li>" )
                    .data( "item.autocomplete", item )
                    .append( "<a>" + item.label + "</a>" )
                    .appendTo( ul );
            };

            $( "<a>" )
                .attr( "tabIndex", -1 )
                //.attr( "title", "Show All Items" )
                .tooltip()
                .appendTo( wrapper )
                .button({
                    icons: {
                        primary: "ui-icon-triangle-1-s"
                    },
                    text: false
                })
                .removeClass( "ui-corner-all" )
                .addClass( "ui-corner-right ui-combobox-toggle" )
                .click(function() {
                    // close if already visible
                    if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
                        input.autocomplete( "close" );
                        removeIfInvalid( input );
                        return;
                    }

                    // work around a bug (likely same cause as #5265)
                    $( this ).blur();

                    // pass empty string as value to search for, displaying all results
                    input.autocomplete( "search", "" );
                    input.focus();
                });                    
        },

        destroy: function() {
            this.wrapper.remove();
            this.element.show();
            $.Widget.prototype.destroy.call( this );
        }
    });
})( jQuery );

$(function() {
    $( "#driver_uuidHOS" ).combobox();       
});
</script>

我声明了两个变量:-

<script src="js/new/jquery-1.8.3.js"></script>
<script>
    var jq13 = jQuery.noConflict(true);
</script>
<script src="js/new/jquery-ui.js"></script>
<script>
    var jq131 = jQuery.noConflict(true);
</script>

所以请告诉我在我的代码中哪里必须使用 jq13 以及 jq131 。此代码用于可搜索的自动建议。 提前致谢。

最佳答案

加载第一个版本后,您可以将其分配给变量。

var jq13 = jQuery.noConflict(true); // as you did

然后加载第二个 jQuery 版本。您加载的第一个可以使用 jq13(...) 访问,而第二个可以使用 $(...) 访问。

无需为第二个脚本分配变量...

关于javascript - Jquery不同版本。 Jquery 可搜索自动建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13928696/

相关文章:

javascript - 如何使脚本在窗口调整大小和方向更改时调整图像和 div 的大小

javascript - 使用下拉菜单解决 jquery 错误

javascript - 将 addClass 与 mouseenter 上的 fadeIn 结合起来

jqueryUI datepicker 并允许非日期?

jquery - 使用飞行效果将一个 Div 飞到另一个 div

javascript - 尝试在 document.ready 中使用 jQuery 在 Bigcommerce Classic Next 主题中触发对产品选项的点击

javascript - 在全局范围内声明的函数是闭包吗?

javascript - 控制台结果不正确?当调用 getUrlParam 函数时

javascript - jQuery.when(deferreds) 或 ZenDesk App 中的等效项

jquery - UI 对话框按钮隐藏控件