我有一个关于javascript的问题。
我必须使用位于html文件头中的javascript代码中的post函数将queryString变量中的数据发送到本地服务器中的autocom.jsp文件,然后使用回调函数( data ){ }从本地数据库中获取数据。
我的问题是,当我在html页面的输入字段中输入c时,测试ok将显示在网页上,即使在我的数据库中,我没有以字母c开头的姓氏列(在数据库中称为last )中的数据。这种情况不应该发生,因为在这种特殊情况下,data.length >0是不正确的。
有人能帮我解决这个问题吗?
谢谢
html文件如下所示: search.html:
<html>
<head>
<link href="styles.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript">
function checkSubmit (thisForm) {
if(thisForm.last.value=='') {
alert('Please Enter Last Name');
return false;
}
}
</script>
<script type="text/javascript">
function lookup(inputString) {
if(inputString.length == 0) {
out.println("testing go");
} else {
$.post("autocom.jsp", {queryString: "" +inputString+ ""}, function(data){
if(data.length >0) {
$('#suggestions').show();
$('#autoSuggestionsList').html(data);
$('#autoSug').html('<h1>testing ok</h1>');
}
});
}
}
</script>
</head>
<body>
<form method="post" action="search.jsp" onsubmit="return checkSubmit (this);">
Enter Last Name: <input type="text" name="last" value="" id="suggestions" id="autoSuggestionsList" onkeyup="lookup(this.value);" /><br>
<input type="submit" value="Submit">
</form>
<p id="autoSug"> </p>
<form method="post" action="home.html">
<input type="submit" value="Back To Home">
</form>
</body>
</html>
下面是autocom.jsp文件:
<%@ page language="java" import="java.sql.*" %>
<% response.setContentType("text/html");%>
<%
String str=request.getParameter("queryString");
String connectionURL = "jdbc:mysql://localhost/jsptest";
Connection con;
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(connectionURL, "root", "Dummy123"); // Get a Connection to the database
String sql = "SELECT last FROM employees WHERE last LIKE ' " +str+ " % ' LIMIT 10 "; //Add the data into the database
Statement stm = con.createStatement();
stm.executeQuery(sql);
ResultSet rs= stm.getResultSet();
while (rs.next ()){
out.println( "<i onclick='fill(" + rs.getString("last") + ");'>" +rs.getString("last")+ " </i>" );
}
%>
发布于 2014-05-29 00:08:15
在<% %>
标记之外有新行,所以您的JSP将输出新的行字符。这使得将被放入data
中的字符串比0
字符更长。
https://stackoverflow.com/questions/23928587
复制相似问题