0

下面的代码显示从数据库填充的列表(不包括数据库代码)。如果我单击搜索结果,则所选选项将不会显示在文本框中。请帮忙。

下面的代码可以很好地在键入字符时显示数据库中的选项列表。问题是它不会将选定的下拉选项显示到文本框中。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery Auto Complete</title>
<script src="jquery/jquery.js" type="text/javascript"></script>

<script type="text/javascript">
function lookup(inputString) {
if(inputString.length == 0) {
$('#suggestions').hide();
} else {
$.post("states.jsp", {queryString: ""+inputString+""}, function(data){
if(data.length >0) {
$('#suggestions').show();
$('#autoSuggestionsList').html(data);
}
});
}
}
function fill(thisValue) {
$('#inputString').val(thisValue);
setTimeout("$('#suggestions').hide();", 200);
}
</script>
<style type="text/css">
body {
font-family: Helvetica;
font-size: 13px;
color: #000;
}
h3 {
margin: 0px;
padding: 0px;
}
.suggestionsBox {
position: relative;
left: 260px;
margin: 0px 0px 0px 0px;
width: 200px;
background-color: #7845DD;
-moz-border-radius: 7px;
-webkit-border-radius: 7px;
border: 2px solid #000;
color: #fff;
}
.suggestionList {
margin: 0px;
padding: 0px;
}
.suggestionList li {
margin: 0px 0px 3px 0px;
padding: 3px;
cursor: pointer;
}
.suggestionList li:hover {
background-color: #DD45CD;
}
</style>
</head>
<body>
<div>
<form>
<div> <h3><font color="red">Indian States</states></font></h3>
<br /> Enter India State Name to see autocomplete
<input type="text" size="30" value="" id="inputString"
onkeyup="lookup(this.value);" onblur="fill();" />
</div>
<div class="suggestionsBox" id="suggestions" style="display: none;">
<div class="suggestionList" id="autoSuggestionsList">
</div>
</div>
</form>
</div>
</body>
</html>



<%
String name=request.getParameter("queryString");
try {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
    Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost", "user", "password");
    Statement st=con.createStatement();
//Add the data into the database
String sql = "SELECT EMP_EMPLOYEE_ID, EMP_FNAME, EMP_LNAME FROM UAP_EMPLOYEE where EMP_FNAME     LIKE '%"+name+"%' OR EMP_LNAME LIKE '%"+name+"%';"; 
Statement stm = con.createStatement();
stm.executeQuery(sql);
ResultSet rs= stm.getResultSet();
while (rs.next ()){
out.println("<li onclick='fill("+rs.getString("EMP_FNAME")+");>"+rs.getString("EMP_FNAME")+"    </i>");
}}catch(Exception e){
out.println("Exception is ;"+e);
}
%>
4

1 回答 1

0

在这一行

$('#autoSuggestionsList').html(data);

您正在设置一些 html,我假设这些 html 包含div带有您的自动建议的标签列表。如果您希望用户能够单击列表,则需要将click处理程序附加到加载该值的建议。喜欢

$(document).ready(function() {
   $("#autoSuggestionsList div").on("click", function() {
      //put your click actions here.. load the textbox, hide the list
   });
}

on命令是实时的,这意味着它将自动绑定到动态加载的元素。

编辑

由于您使用li而不是div,将上面的内容更改为

$(document).ready(function() {
   $("#autoSuggestionsList li").on("click", function() {
      fill($(this).text());
   });
}

编辑 2

在您添加的新代码中,此行不正确

out.println("<li onclick='fill("+rs.getString("EMP_FNAME")+");>"+rs.getString("EMP_FNAME")+"    </i>");

它将产生如下所示的错误 HTML/Javascript:

<li onclick='fill(some text);>some text</i>

您的代码应如下所示:

out.println("<li onclick='fill(\""+rs.getString("EMP_FNAME")+"\");'>"+rs.getString("EMP_FNAME")+"    </li>");

它将产生以下正确的 HTML/JavascriptL

<li onclick='fill("some text");'>some text</li>

于 2012-08-11T21:47:14.807 回答