我已经看到了很多关于自动完成的问题和答案,但没有一个特定于我正在做的事情。我要做的是让用户从选择菜单中选择一个选项(即 SSN、名字、姓氏、出生日期),这当然会命中数据库并填满相应的数据。当用户开始在文本框中输入字符时,自动完成功能通过显示正确的数据开始工作。我有一个可以正常工作的自动完成功能,问题是 -
如何根据所选的选择菜单值确定要访问的数据?
HTML
<div id="search-title"> Employees</div>
<div id="search-container-large">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<label for="search-text" class="search-label">Search By:</label>
<select name="searchkey" size="1" id="searchkey" class="dropDown">
<option value="0" selected> </option>
<option value="2" >SSN</option>
<option value="4" >Last Name</option>
<option value="3" >First Name</option>
<option value="5" >ID</option>
<option value="6" >Number</option>
</select>
<input name="searchvalue" type="text" class="form-field-search" id="searchvalue"/><a href="#"><img src="img/search-btn.png" width="28" height="24" border="0" class="search-btn"/></a>
</td>
</tr>
</table>
</div>
JAVASCRIPT
jQuery(function() {
$("#searchvalue").autocomplete("http://localhost/test/getData.jsp", {
extraParams: {
filter: getDropdownValue()
}
});
});
function getDropdownValue() {
var compId=document.getElementById("searchkey").value;
return compId;
}
JSP
<%
DummyDB db = new DummyDB();
String name=request.getParameter("filter");
String query = request.getParameter("q");
List<String> fnames = db.getData(query);
Iterator<String> iterator = fnames.iterator();
while(iterator.hasNext()) {
String searchvalue = (String)iterator.next();
out.println(searchvalue);
}
%>
JAVA文件
public class DummyDB {
private int firstNames;
private String data = "Alan, Albert, Alex, Bain, Brian, Carl, Chris, David, Derek, Frank, Frodo, Gary, Greg, Yaser";
private List<String> fnames;
public DummyDB() {
fnames = new ArrayList<String>();
StringTokenizer st = new StringTokenizer(data, ",");
while(st.hasMoreTokens()) {
fnames.add(st.nextToken().trim());
}
firstNames = fnames.size();
}
public List<String> getData(String query) {
String searchvalue = null;
query = query.toLowerCase();
List<String> matched = new ArrayList<String>();
for(int i=0; i<firstNames; i++) {
searchvalue = fnames.get(i).toLowerCase();
if(searchvalue.startsWith(query)) {
matched.add(fnames.get(i));
}
}
return matched;
}
}