2013-02-22 139 views
0

我想在我的表单上有一个自动完成输入,我有问题获取数据显示。有很多关于此的帖子,但看起来像许多不同的方式。我一直试图按照jQuery网站上的例子,但我想我只是没有得到正确的返回数据?我的页面看起来像:jquery自动完成JSON

<html> 
<head> 
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css" type="text/css" media="all" /> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.js"></script> 

<script type="text/javascript">           
$(document).ready(function() { 
    $("#Codes").autocomplete({ 
     source: function(request, response) { 
      $.ajax({     
        url: "/jreqlib", 
        dataType: "json", 
        data: { 
         featureClass: "P", 
         style: "full", 
         maxRows: 25, 
        }, 
        success: function(data) { 
         response($.map(data, function(item) { 
          return { 
           label: item.name, 
           value: item.name 
          } 
         })); 
        } 
       }); 
     }, 
     minLength: 1, 
     open: function() { 
     $(this).removeClass("ui-corner-all").addClass("ui-corner-top"); 
     }, 
     close: function() { 
      $(this).removeClass("ui-corner-top").addClass("ui-corner-all"); 
     } 
    }); 
}); 
    </script> 
</head> 
<body> 
<form> 
    <input id="Codes"> 
</form> 
</body> 
</html> 

我是从服务器返回什么看起来是这样的:

[{"1234":"1234"},{"134":"134},{"567":"567"}] 

我喜欢当我在框中单击是,它让我看到“1234”和“567”,如果我输入1,则会出现“1234”和“134”,如果我输入12,那么只会出现“1234”等。

任何帮助,将不胜感激 TIA

+0

我没有使用jQuery的从自动完成它看起来像你正在尝试使用项目之前。名称,但您的json不包含名称。 – BlueBird 2013-02-22 18:59:17

+0

我试过改变json,所以它是[{“name”:“1234”},{“name”:“134}等,但仍然没有去:( – Analog 2013-02-22 22:32:18

回答

0

我的建议是,让服务器生成的JSON动态取决于搜索字符串。在maxRows: 25,以下插入name_startsWith: request.term。服务器现在可以在name_startsWith获取变量中找到搜索字符串。对于搜索字符串'1',服务器可能会以["1234","134"]进行响应。

此外,删除

featureClass: "P", 
style: "full", 
maxRows: 25, 

而更换

response($.map(data, function(item) { 
    return { 
     label: item.name, 
     value: item.name 
    } 
})); 

通过

response(data);