有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javascript Jquery自动完成不向java传递值

我已经使用jqueryui库创建了autocomplete,并尝试在java中获取文本框值,但没有获取值,而是获取null值。请帮助从文本框中获取值。这是行String query = (String)request.getParameter("country");没有获取值吗

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <style>
        input {
            font-size: 120%;    }
    </style>
</head>
<body>
        <h3>Feature</h3>
    <input type="text" id="country" name="country"/>
            <script>
        //$("#country").autocomplete("getdata.jsp");
        $("#country").autocomplete({
source: "getdata.jsp",
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
}
});
    </script>
        </body>
</html>

获取数据。jsp

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

   <%
  String query = (String)request.getParameter("country");
  System.out.println("query"+query);
   try{
     String s[]=null;
     Class.forName("oracle.jdbc.driver.OracleDriver");
     Connection con =DriverManager.getConnection("XXXXX");
     Statement st=con.createStatement();
     ResultSet rs = st.executeQuery("select name from table1 where name like '"+query+"%'");
     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
       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();
}
 %>

共 (1) 个答案

  1. # 1 楼答案

    它不是一个表单,所以不要使用getParameter()获取值

    source: "getdata.jsp?country="+$("#country").val(),