如何进行应该来自数据库的自动建议,一旦选择它应该自动填充文本框?

时间:2014-12-27 12:26:20

标签: javascript jquery mysql jsp autocomplete

suggesstion文本框,其中数据应来自数据库,一旦选中,它应自动填充数据库。如何在Jquery中使用自动完成功能。这是我的代码,

的index.jsp `

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="jquery-ui.css" />
    <script type="text/javascript" src="jquery-1.11.2.min.js"></script>
    <script src="JS/jquery.autocomplete.js"></script>
    <script>

  function myFun() {


   //$.getJSON( "list.jsp",{q : $("#country").val()}, displayResult);
   $.ajax({type:"POST",url:"list.jsp",
   data:"q="+document.getElementById("country").value,
   sucess:function(data){$("#txthint").html(data);}});
};

</script>
</head>
<body>
    <div style="width: 300px; margin: 50px auto;">
        <b>Country</b>   : <input type="text" id="country" name="country" 
                        class="input_text" onkeyup="myFun()"/>

      <p id="txtHint"></p>
</body>
</html>`

的List.jsp

`

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

<% 
    out.println("came");
    try{
Class.forName("com.mysql.jdbc.Driver");
Connection connection;
       connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
   Statement statement = connection.createStatement() ;
       ResultSet rs;
      rs =statement.executeQuery("select * from sss where uname like '%"+request.getParameter("q")+"%';");

       while(rs.next())
       {

       out.println(rs.getString(1));

       }
    }
    catch(Exception e)
        {
             out.println("wrong entry"+e);
        }

%>

`

提前致谢。

0 个答案:

没有答案