2012-10-21 32 views
0

我試圖在我的搜索字段上實現自動完成。使用JSP文件作爲源的jQuery自動完成問題

<%@ page language="java"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
    <title>Basic Search</title> 
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css" /> 
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.0/themes/base/jquery- ui.css" /> 
    <link rel="stylesheet" type="text/css" href="css/jquery.catcomplete.css" /> 

    <script type="text/javascript" src="js/bootstrap.js"></script>  
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"> </script> 
    <script type="text/javascript" src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script> 

</head> 
<body> 

<script> 
$.widget("custom.catcomplete", $.ui.autocomplete, { 
    _renderMenu: function(ul, items) { 
     var that = this, 
      currentCategory = ""; 
     $.each(items, function(index, item) { 
      if (item.category != currentCategory) { 
       ul.append("<li class='ui-autocomplete-category'>" + item.category + "</li>"); 
       currentCategory = item.category; 
      } 
      that._renderItemData(ul, item); 
     }); 
    } 
}); 

$(function() { 

    var data = [ 
       { label: "Eric Cartman", category: "Users" }, 
       { label: "John Lennon", category: "Users" }, 
       { label: "Elvis Presley", category: "Users" }, 
       { label: "Steven Gerrard", category: "Users" }, 
       { label: "Java", category: "Programming" }, 
       { label: "C#", category: "Programming" }, 
       { label: "C++", category: "Programming" }, 
       { label: "Python", category: "Programming" }, 
       { label: "Ruby on Rails", category: "Programming" } 
      ]; 
    $("#search").catcomplete({ 
     delay: 0, 
     minLength: 3, 
     source: data 
    }); 
}); 
</script> 

<p /> 

<div class="container well"> 
    <div class="row-fluid"> 
     <form action="search" method="get"> 
      <div class="input-append"> 
       <input type="text" name="query" id="search" placeholder="Search.."/> 
       <button class="btn" type="submit">Search</button> 
      </div> 

     </form> 
    </div> 
</div>  

</body> 
</html> 

然而,當我嘗試實現:當我用源是一個簡單的JavaScript陣列(見VAR數據= [...])這樣,一切工作正常

<%@ page language="java"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Basic Search</title> 
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css" /> 
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.0/themes/base/jquery-ui.css" /> 
    <link rel="stylesheet" type="text/css" href="css/jquery.catcomplete.css" /> 

    <script type="text/javascript" src="js/bootstrap.js"></script>  
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script> 
    <script type="text/javascript" src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script> 

</head> 
<body> 

<script> 
    $.widget("custom.catcomplete", $.ui.autocomplete, { 
    _renderMenu: function(ul, items) { 
     var that = this, 
      currentCategory = ""; 
     $.each(items, function(index, item) { 
      if (item.category != currentCategory) { 
       ul.append("<li class='ui-autocomplete-category'>" + item.category + "</li>"); 
       currentCategory = item.category; 
      } 
      that._renderItemData(ul, item); 
     }); 
    } 
}); 
</script> 

<script type="text/javascript"> 
    $(function() { 
    $("#search").catcomplete({ 
     delay: 0, 
     minLength: 3, 
     source: 'getsearchjson.jsp' 
    }); 
}); 
</script> 

<p /> 

<div class="container well"> 
    <div class="row-fluid"> 
     <form action="search" method="get"> 
      <div class="input-append"> 
       <input type="text" name="query" id="search" placeholder="Search.."/> 
       <button class="btn" type="submit">Search</button> 
      </div> 

     </form> 
    </div> 
</div> 

</body> 
</html> 

getsearchjson.jsp:

用JSP文件作爲這樣的源完全一樣的東西
<% 
    response.setContentType("application/json"); 
%> 
[ 
    { label: "Eric Cartman", category: "Users" }, 
    { label: "John Lennon", category: "Users" }, 
    { label: "Elvis Presley", category: "Users" }, 
    { label: "Steven Gerrard", category: "Users" }, 
    { label: "Java", category: "Programming" }, 
    { label: "C#", category: "Programming" }, 
    { label: "C++", category: "Programming" }, 
    { label: "Python", category: "Programming" }, 
    { label: "Ruby on Rails", category: "Programming" } 
]; 

它停止工作,即自動完成功能不再出現在我的搜索字段中。

澄清:第一個實現和第二個實現之間的唯一區別是數據的來源。在第一個實現中,它是一個JavaScript數組,在第二個實現中它是一個JSP文件。

感謝您的回覆。爲了您的信息,我的JavaScript知識是非常有限的,因爲它不是我大學課程的一部分。但是,我想用它來使Web應用程序看起來更專業。

+0

檢查URL是否正確。使用Firebug或Chrome開發人員工具查看是否將請求發送到JSP,以及響應是什麼。 –

+0

感謝您的快速回復。通過使用Chrome開發人員工具,我可以看到正在發送一個請求,並且該響應實際上是來自getsearchjson.jsp文件的JSON數據。 –

回答

0

我認爲問題在於您的JSP不包含有效的JSON數據。鑰匙應該用雙引號括起來,使之有效的JSON:

[ 
    { "label": "Eric Cartman", "category": "Users" }, 
    { "label": "John Lennon", "category": "Users" }, 
    ... 

使用jsonlint.com檢查您的JSON的有效性。

+0

謝謝!這似乎是問題所在。在我將引號括起來並在getsearchjson.jsp結尾處刪除分號後,我已經接近解決方案。當我按照預期輸入搜索字段時,我現在正在獲得建議,但是,無論我輸入什麼內容,我都會在getsearchjson.jsp中獲取_everything_。任何想法如何繼續?我懷疑問題出在第一個