jQuery UI自动完成自定义搜索

问题描述:

我目前在我的应用程序中使用了jQuery UI自动完成功能,我想通过将结果中的最后一个单词转换为不同的颜色(比如蓝色)来自定义结果的设计。为此,我已经使用http://jqueryui.com/autocomplete/#custom-data如下:jQuery UI自动完成自定义搜索

$(input).autocomplete(config) 
     .data("ui-autocomplete")._renderItem = function (ul, item) { 
        return $("<li>").append("<a>" + item.label + " " + "<span class=\"blue\">" + item.explicitLabel + "</span>" + "</a>").appendTo(ul); 
       }; 

哪里item.label是不硬道理item.explicitLabel自动完成结果才是硬道理。我唯一的问题是,搜索时,最后一个字(explicitLabel)被忽略。这里是一个例子:http://jsfiddle.net/japZB/。我需要做什么才能搜索完整的输出结果?

+1

可能的重复http://stackoverflow.com/questions/15846710/jquery-ui-autocomplete-search-from-multiple-attributes-of-one-array – billyonecan 2013-05-10 13:37:16

+0

是的,没有。他有一个更复杂的问题。其实,我的问题的答案是在他的问题。 :) – 2013-05-10 13:43:32

更直接的一种简单的方法将增加额外的字段全文

var list = []; 
list.push({ 
    label: "This is a good test", partialLabel: "This is a good", explicitLabel: "test" 
}); 
list.push({ 
    label: "This is a bad test", partialLabel: "This is a bad", explicitLabel: "test" 
}); 
list.push({ 
label: "This is a bad nice day", partialLabel: "This is a nice", explicitLabel: "day" 
}); 

但是,这是在我看来矫枉过正。如果你拥有源列表格式,你可以有一些像这样简单

var list = []; 
list.push("This is a good test"); 
list.push("This is a bad test"); 
list.push("This is a bad nice day"); 

而且从字符串获得最后一个单词使用字符串操作来颜色故。 lastIndexOf将有助于获得最后的空白区域(如果有的话)

+0

这就是当你跳入代码而没有研究文档时会发生什么。错过了关于'label'和'value'属性的整个部分。使用第一个解决方案,因为第二个解决方案没有真正应用(我有一个更复杂的实际情况,这个例子)。谢谢! – 2013-05-10 13:42:33