Jquery Autocomplete没有将值传递给java

时间:2013-11-06 00:25:03

标签: java javascript jsp jquery

我已经使用Jquery UI库创建了自动完成,并尝试在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>

getdata.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 个答案:

答案 0 :(得分:0)

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

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