java ajax输入框自动提示

sp代码:

<html:text
                                                property="startBook.brandName" 
                                                style="210px;"  styleId="brand" />
                                        <div id="brandlist" style="overflow: auto; display:none; 210px; height:150px;  position: absolute; text-align:left; background-color: white;  border: solid thin #000;">


</div>

jquery:

$(function(){
    $("#brand").keyup(function(){
    $("#brandlist").show();
    var brand=$(this).val();
    $.ajax({
            url : "startUpBook.go?method=brandlist",
            type : "POST",
            cache : false,
            data : "brand="+brand,
            error : function() {
                alert("数据加载异常")
            },
            success : function(data) {
            
                $("#brandlist").html(data);
                
            }
        });
    })});


action:

 String brand=request.getParameter("brand").toString().trim();
         System.out.print(brand);
        String brandl=startBook.brandlist(brand);
        System.out.print(brandl);
        request.setAttribute("data", brandl);
        response.setContentType("textml;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");    
        response.getWriter().print(brandl);
        return null;

IBAS:

public String brandlist(String brand) {
        String brandlisturl="";
        List list = new ArrayList();
        String name=brand;
        list = this.queryList(name, "XNN_sql_Product.queryBrandlist");
        for(int i=0;i<list.size();i++)
        {    CBrand cbrand=(CBrand)list.get(i); 
           brandlisturl=brandlisturl+"<p onclick="+""$('#brand').val($(this).html());$('#brandlist').hide();">"+cbrand.getName()+"</p>";        
        }

        return brandlisturl;


SQL:

SELECT DISTINCT(name) `name`  from c_brand WHERE 1=1 
        <isNotEmpty prepend=" and ">
            name like
            CONCAT('%', '$name$', '%')
        </isNotEmpty>
 and name is not null


效果实现中遇到问题:

SQL 传参数没有加‘号,查询出错

界面结果无法实现点击后自动到输入框。

解决办法 在查询阶段把JS事件拼进去。(不足代码不简介,拼方法进去,调用不成功)

JQUERY里KEYUP,blur事件在同一级,无法实现再次查询后的结果自动填充

后期时间允许再做代码优化

原文地址:https://www.cnblogs.com/lishoubin/p/3455057.html