admin管理员组

文章数量:1290979

i am creating a web project using JSP, and is trying to implement a simple search for users from my database using jquery autoplete, however i am having trouble understanding how it works. i have little to no knowledge on jquery and ajax just to let you know. i have done the following code and am stuck.

<%@page contentType="text/html" pageEncoding="UTF-8" import="ewa.dbConnect,ewa.sendEmail,ewa.pwGen,ewa.hashPw,java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   ".dtd">

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/jquery.autoplete.css" />
        <script src="js/jquery.autoplete.js"></script>
        <script type="text/javascript"
            src=".4.4/jquery.min.js"></script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <input type="text" id="search" name="search"/>
        <script>
        $("#search").autoplete("getdata.jsp");
    </script>
    </body>
</html>

getdata.jsp

<%@page contentType="text/html" pageEncoding="UTF-8" import="ewa.dbConnect,java.sql.*" %>
<%! dbConnect db = new dbConnect(); %>
<%
String query = request.getParameter("q");
db.connect();
Statement stmt = db.getConnection().createStatement();
ResultSet rs = stmt.executeQuery("SELECT username FROM created_accounts WHERE username LIKE "+query);
while(rs.next())
{
    out.println(rs.getString("username"));
}
db.disconnect
%>

if i am not wrong i read from a website, the parameter q is default and is just there, however how do i display the data? how do i pass the values from getdata.jsp into the autoplete?

i am creating a web project using JSP, and is trying to implement a simple search for users from my database using jquery autoplete, however i am having trouble understanding how it works. i have little to no knowledge on jquery and ajax just to let you know. i have done the following code and am stuck.

<%@page contentType="text/html" pageEncoding="UTF-8" import="ewa.dbConnect,ewa.sendEmail,ewa.pwGen,ewa.hashPw,java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3/TR/html4/loose.dtd">

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/jquery.autoplete.css" />
        <script src="js/jquery.autoplete.js"></script>
        <script type="text/javascript"
            src="https://ajax.googleapis./ajax/libs/jquery/1.4.4/jquery.min.js"></script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <input type="text" id="search" name="search"/>
        <script>
        $("#search").autoplete("getdata.jsp");
    </script>
    </body>
</html>

getdata.jsp

<%@page contentType="text/html" pageEncoding="UTF-8" import="ewa.dbConnect,java.sql.*" %>
<%! dbConnect db = new dbConnect(); %>
<%
String query = request.getParameter("q");
db.connect();
Statement stmt = db.getConnection().createStatement();
ResultSet rs = stmt.executeQuery("SELECT username FROM created_accounts WHERE username LIKE "+query);
while(rs.next())
{
    out.println(rs.getString("username"));
}
db.disconnect
%>

if i am not wrong i read from a website, the parameter q is default and is just there, however how do i display the data? how do i pass the values from getdata.jsp into the autoplete?

Share Improve this question edited Jun 12, 2013 at 13:00 Yahel 37.3k23 gold badges106 silver badges154 bronze badges asked Jan 9, 2011 at 14:28 sutoLsutoL 1,76711 gold badges28 silver badges49 bronze badges 2
  • 1 there should be 2 files, that may be helpful: viralpatel/blogs/2009/06/… – Infinity Commented Jan 9, 2011 at 14:46
  • hi that is the site i went it, but it did not provide enough documentation, and the code is two code somehow it did not get separated – sutoL Commented Jan 9, 2011 at 15:32
Add a ment  | 

3 Answers 3

Reset to default 7

You're calling the autoplete script tag before jQuery has been included. So, not having jQuery to latch onto (as the jQuery object hasn't been defined), nothing from the jQuery autoplete plugin will load.

You have

 <script src="js/jquery.autoplete.js"></script>
 <script type="text/javascript"
     src="https://ajax.googleapis./ajax/libs/jquery/1.4.4/jquery.min.js"></script>

It should be

    <script type="text/javascript"
        src="https://ajax.googleapis./ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script src="js/jquery.autoplete.js"></script>

Reverse the order, and the Firebug errors you mentioned should disappear; I'm not sure it'll solve everything, but nothing will work until that's resolved.

I don't see jQuery UI being included (that one provides the autoplete functionality)

http://jqueryui./demos/autoplete/

So you need to include jquery.ui.autoplete.js (Or are you using the plugin autoplete? if so, move to the jquery UI version)

Could also be that the data from getdata.jsp is malformed for the use in autoplete.

How you tried debugging the javascript in a browser such as chrome or in firefox(with firebug)

I usual give (for jquery UI autoplete) a JSON formatted answer, while I see your answer loop give a CR delimited list.

In getdata.jsp instead of produce:

jim<cr>
jack>cr>
jhon<cr>

try to return:

[{label: 'jim', value: 'jim'}, {label:
 'jack', value: 'jack'}, {label:
 'jhon', value: 'jhon'}]

本文标签: javahow to use jquery autocompleteStack Overflow