gpt4 book ai didi

java - 使用 jquery 在我的表 tbcntry 中自动完成 jsp 形式的国家文本框

转载 作者:行者123 更新时间:2023-11-30 10:34:33 25 4
gpt4 key购买 nike

//下面是我的表单页面,其中包含国家/地区的文本框我想显示所有以字母开头的国家/地区并自动完成我数据库中的值以及使用 jsp、java、jquery 的国家/地区数据

<html>
<head>
<link href = "https://code.jquery.com/ui/1.10.4/themes/uilightness/jquery- ui.css"rel = "stylesheet">
<script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
<script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script>
$(document).ready(function() {
$('#country').focusin(function() {
$("#country").autocomplete("List.jsp");
});
});
</script>
</head>
<body>
<br><br><center>
<font face="verdana" size="2">
<font size="4">Java(jsp)/jQuery Autocompleter Example</font>
<br><br><br><br>

Select Country :
<input type="text" id="country" name="country"/>

</font>
</body>
</html>

//下面是连接数据库的List.jsp页面

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.*"%>

<%
try {
String s[] = null;

Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver found");
String url = "jdbc:mysql://localhost:protNum/dbName";
String user = "";
String pass = "";
System.out.println("Connected....");
Connection con = (Connection) DriverManager.getConnection(url, user, pass);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from tbctry");

List li = new ArrayList();

while (rs.next()) {
li.add(rs.getString(1));
}

String[] str = new String[li.size()];
Iterator it = li.iterator();

int i = 0;
while (it.hasNext()) {
String p = (String) it.next();
str[i] = p;
i++;
}

//jQuery related start
String query = (String) request.getParameter("q");

int cnt = 1;
for (int j = 0; j < str.length; j++) {
if (str[j].toUpperCase().startsWith(query.toUpperCase())) {
out.print(str[j] + "\n");
if (cnt >= 5)// 5=How many results have to show while we are typing(auto suggestions)
{
break;
}
cnt++;
}
}
//jQuery related end

rs.close();
st.close();
con.close();

} catch (Exception e) {
e.printStackTrace();
}
%>

最佳答案

//下面是使用json和jquery实现autocomplete//Jsp 表单页面,用于在用户输入第一个字母时将国家名称作为文本框输入,自动完成功能开始运行

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Autocomplete in java web application using Jquery and JSON</title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script>
$(document).ready(function() {
$(function() {
$("#search").autocomplete({
source: function(request, response) {
$.ajax({
url: "CountryListCheck",
type: "GET",
data: {
term: request.term
},
dataType: "json",
success: function(data) {
response(data);
}
});
}
});
});
});
</script>


</head>
<body>
<div class="header">
<h3>Autocomplete in java web application using Jquery and JSON</h3>
</div>
<br />
<br />
<div class="search-container">
<div class="ui-widget">
country:
<input type="text" id="search" name="search" class="search" />
</div>
</div>
</body>
</html>

//下面是url CountryListCheck对应controller的代码

 "/CountryListCheck":
String term = request.getParameter("term");
System.out.println("Data from ajax call " + term);
ArrayList<String> list = new Op_contact(dcon).getAllCountriesCheck(term);
System.out.println(list);
Json = new Gson().toJson(list);
response.getWriter().write(Json );

//下面是操作类代码或数据库查询部分

public ArrayList<String> getAllCountriesCheck(String l) {
ArrayList<String> list = new ArrayList<String>();
PreparedStatement ps = null;
String data;
try {
String ch=l+"%";
ps = (PreparedStatement) dcon.con.prepareStatement("SELECT nicename FROM tbctry WHERE nicename LIKE '"+ch+"'");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
data = rs.getString("nicename");
System.out.println(data);
list.add(data);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
return list;
}

关于java - 使用 jquery 在我的表 tbcntry 中自动完成 jsp 形式的国家文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41606227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com