百度下拉框

jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

    <script src="js/jquery-1.8.3.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function() {
            $("#name").keyup(function(){
            document.getElementById("mess").style.display="block";
            //$("#mess").attr("display","block");
            //alert("1234")
                $.ajax({
                    url : "ajaxServlet", //要提交的URL路径
                    type : "post",      //发送请求的方式
                    data : {ser:$("#name").val()}, //要发送到服务器的数据
                    dataType : "text",    //指定传输的数据格式
                    success : function(result) {//请求成功后要执行的代码
                        var arr = result.split(",");
                        //alert("1");
                        $("#mess").html("");
                        for(var i=0;i<arr.length;i++){
                            $("#mess").append("<div class='mouse' onmouseover='mouseover(this);' onmouseout='mouseout(this);' onclick='click1(this);'>"+arr[i]+"</div>");
                        }
                    },
                    error : function() { //请求失败后要执行的代码
                        alert("验证失败!!");
                    }
                });
            });
        });
    function mouseover(v){
        v.style.backgroundColor="#F4F2F4";
    }
    function mouseout(v){
        v.style.backgroundColor="white";
    }   
    function click1(v){
        var value = v.innerHTML;
        $("#name").val(value);
        $("#mess").html("");

    }

    </script>
  <style type="text/css">
        body{width: 1024; }
        table{margin-top: 50px; width: 70%}
        tr{height: 50px}
        .t1{ background-color:#DFFFFF }
        #name{ width: 400px;}
        #mess{position: absolute; top: 87px;left: 390px;display: none}
    </style>
  </head>

  <body>
   <form action="">
    <table align="center">
        <tr>
        <td class="t1" width="25%">承租人名称</td>
        <td class="t1" width="45%">
        <input id="name" type="text"  > 

        </td>
        </tr>
        <tr>
        <td class="t1" width="25%">友情提示</td>
        <td class="t1" width="45%"> </td>
        </tr>
        <tr>
        <td class="t1" width="25px">是否锁定</td>
        <td class="t1" width="45%"> </td>
        </tr>
    </table>
    <div id="mess" style="width: 400px; float: left;"></div>
   </form>

  </body>
</html>

servlet

package com.lq.servlet;

import java.io.IOException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lq.utils.JdbcUtil;

public class AjaxServlet extends HttpServlet {

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doPost(request, response);
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String ser= request.getParameter("ser");
        System.out.println(ser);
        JdbcUtil ju=new JdbcUtil();
        String sql="select name,card from t_users t where name like '%"+ser+"%'";
        System.out.println(sql);
        //List list=new ArrayList();
        String s="";
        try {
            ResultSet rs=ju.querry(sql, null);
            while(rs.next()){
                System.out.println(rs.getString(1)+"_"+rs.getString(2)+",");
                s=s+(rs.getString(1)+"_"+rs.getString(2)+",");
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        response.getWriter().print(s);

    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值