2012-10-15 38 views
0

我将帖子绑定到位置,因此当我在该位置上创建jQuery自动完成时,具有相同名称的字段结果出现在下拉列表中。具有相同名称的jQuery自动完成组结果

例如:

如果有10个职位与位置柏林和我开始输入城市名称的自动完成就会给我乘的职位数的位置的名称。我想知道是否有一种方法可以使用jQuery对位置名称进行分组。

感谢

回答

2

This jquery ui demo类似于您的要求。

原始数据:

var data = [ 
       { label: "anders", category: "" }, 
       { label: "andreas", category: "" }, 
       { label: "antal", category: "" }, 
       { label: "annhhx10", category: "Products" }, 
       { label: "annk K12", category: "Products" }, 
       { label: "annttop C13", category: "Products" }, 
       { label: "anders andersson", category: "People" }, 
       { label: "andreas andersson", category: "People" }, 
       { label: "andreas johnson", category: "People" } 
      ]; 

,你可以将其更改为

var data = [ 
       { label: "Berlin", category: [post1, post2] }, 
       { label: "Paris", category: [post3, post4, post5] }, 
       { label: "Tokyo", category: [post6, post7, post8] }, 
       { label: "Taipei", category: [] } 
       ... 
     ]; 

检索值

select: function(event, ui) { 
       console.log(ui.item.label); 
       console.log(ui.item.category); //this is an array 
       return false; 
} 

而且http://jqueryui.com/autocomplete/#custom-data 是关于dropmenu的自定义数据格式演示

我希望这个示例有帮助。