---
title: "Jquery UI autoComplete 设置max "
author: "wangrl"
date: "2012-11-11T20:01:22Z"
tags: []
categories: ["Web前端"]
slug: "jquery-ui-autocomplete-max"
draft: false
lastmod: "2012-11-11T20:01:22Z"
id: 1722555
url: "https://wangrl.iteye.com/blog/1722555"
---

转自：http://blog.csdn.net/cdefg198/article/details/8115336

 

好像新版的Jquery UI,autoComplete的options里面没有max，不知道是不是我没有知道，如果数据量很大，有可能显示非常。所以修改了一下代码，定死了，没有添加到options里面去。

在Jquery-ui.js的6659行代码处，\_normalize函数里面。

原来是：

**\[javascript]** [view plain](http://blog.csdn.net/cdefg198/article/details/8115336 "view plain") [copy](http://blog.csdn.net/cdefg198/article/details/8115336 "copy")

01. \_normalize:  function ( items ) {
02. // assume all items have the right format when the first item is complete
03. if  ( items.length && items\[0].label && items\[0].value ) {
04. return  items;
05. }
06. return  $.map( items,  function ( item ) {
07. if  (  typeof  item ===  "string"  ) {
08. return  {
09. label: item,
10. value: item
11. };
12. }
13. return  $.extend({
14. label: item.label || item.value,
15. value: item.value || item.label
16. }, item );
17. });
18. },

修改为：

**\[javascript]** [view plain](http://blog.csdn.net/cdefg198/article/details/8115336 "view plain") [copy](http://blog.csdn.net/cdefg198/article/details/8115336 "copy")

01. \_normalize:  function ( items ) {
02. // assume all items have the right format when the first item is complete
03. if  ( items.length && items\[0].label && items\[0].value ) {
04. return  items;
05. }
06. var  i=0;
07. return  $.map( items,  function ( item ) {
08. if (i &gt;= 20 )
09. return ;
10. i++;
11. if  (  typeof  item ===  "string"  ) {
12. return  {
13. label: item,
14. value: item
15. };
16. }
17. return  $.extend({
18. label: item.label || item.value,
19. value: item.value || item.label
20. }, item );
21. });
22. },

最大显示20条