jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery Autcomplete如果有源匹配,我怎么能总是显示renderMenu?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想为我的jQuery自动完成添加一个帮助程序,如下所示:

var thing = $("#thing").autocomplete({
        minLength: 0,source: myarray
    })

    // Override the Render Menu
    thing.data("autocomplete")._renderMenu= function(ul,items) {
        var self = this;
        $.each( items,function( index,item ) {
            self._renderItem( ul,item );
        });
        // Adder
        ul.append( "<a class='Helper'>Add <b>\"" + this.term + "\"</b> as a new item</a>")
    }

问题是这个HelPER仅在自动填充与myarray至少有1次搜索匹配时显示.如何在用户聚焦时始终显示菜单

谢谢

解决方法

@H_502_20@ 我认为还需要更多的猴子补丁:

/* Override the _response function to always open the suggestions menu: */
thing.data("autocomplete")._response = function(content) {        
    if (!this.options.disabled) {
        this._trigger("open");
        content = this._normalize(content);
        this._suggest(content);
    }

    this.pending--;
    if (!this.pending) {
        this.element.removeClass("ui-autocomplete-loading");
    }
};

例:http://jsfiddle.net/eJMuf/

这应该有效,但我将继续寻找使用标准API的解决方案.

这是另一种不需要开心手术的解决方案,但需要更多代码

var thing = $("#thing").autocomplete({
    minLength: 0,/* Use a source function instead of the array */
    source: function(request,responsE) {
        var result = myArray.slice(0);
        /* Filter the array normally... */
        result = $.ui.autocomplete.filter(result,request.term);

        /* Add a placeholder result that we can process later */
        result.push({
            value: request.term,isPlaceholder: true
        });
        response(result);

    }
}).focus(function() {
    $(this).autocomplete("search",this.value);
});

var renderItem = thing.data("autocomplete")._renderItem;
/* Override the _renderItem function to display the placeholder item */
thing.data("autocomplete")._renderItem = function(ul,item) {
    if (item.isPlaceholder) {
        return $("<li>")
            .data("item.autocomplete",item)
            .append("<a class='Helper'>Add <b>\"" + item.value + "\"</b> as a new item</a>")
            .appendTo(ul);

    } else {
        renderItem(ul,item);
    }
};

例:http://jsfiddle.net/FvCY6/

我个人会采用这种解决方案,因为它的侵入性较小.

大佬总结

以上是大佬教程为你收集整理的jquery Autcomplete如果有源匹配,我怎么能总是显示renderMenu?全部内容,希望文章能够帮你解决jquery Autcomplete如果有源匹配,我怎么能总是显示renderMenu?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。