2013-02-22 138 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);