2015-07-13 52 views
0

这里是我的代码:如何使用Ajax和jQuery在JSP

<%@ page language="java" contentType="text/html; charset=UTF-8" 
pageEncoding="UTF-8"%> 
    <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> 
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
    <!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=UTF-8"> 
    <title>Insert title here</title> 
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> 
<script src="//code.jquery.com/jquery-1.10.2.js"></script> 
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 

</head> 
<body> 
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" 
url="jdbc:mysql://localhost/demo" 
user="root" password="root"/> 

<sql:query dataSource="${snapshot}" var="list"> 
    SELECT * from EmoloyeeSample; 
</sql:query> 
<form action="some.jsp" method="get"> 
<select name="id" id="name" onkeyup="doSomething(this.value);"> 

    <option value="0">Select an Employee</option> 
    <c:forEach var="employee" items="${list.rows}"> 
    <option value="${employee.id}">${employee.name}</option> 
    </c:forEach> 


</select> 
<input type="submit" value="Submit" > 

<script> 
$(function() { 
var availableTags=${list} 
$("#name").autocomplete({ 
    source: availableTags 
}); 
}); 
</script> 
</form> 
</body> 
</html> 

在这里我得到在$ {list}里,所有值从数据库中的值在下拉列表中显示,但我的需求量的是,当用户按s然后只有名称具有初始值,因为s必须显示在下拉列表中。

回答

1

您可以使用一个DataList作为here

请考虑,如果场的数量是巨大的比这可能不是一个很好的解决方案