ITeye博客

jquery自动补全

cpf1985 · 2010-12-21 15:37 · java · 编程语言 · Markdown

可运行的jquery自动补全 jsp部分


      <head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<link href="${ctx}/css/default.css" rel="stylesheet" type="text/css" />
		<link href="${ctx}/css/jquery.autocomplete.css" rel="stylesheet" type="text/css" />
		<script src="${ctx}/js/jquery.autocomplete.js" type="text/javascript"></script>
		

 
<script type="text/javascript">
function findValue(li) {
if( li == null ) return alert("No match!");

// if coming from an AJAX call, let's use the CityId as the value
if( !!li.extra ) var sValue = li.extra[0];

// otherwise, let's just display the value in the text box
else var sValue = li.selectValue;

//alert("The value you selected was: " + sValue);
}

function selectItem(li) {
findValue(li);
}

function formatItem(row) {
return row[0];
}


	function lookupLocal(){
	var oSuggest = $("#autoname")[0].autocompleter;

	oSuggest.findValue();

	return false;
	}

$(function() {

$("#autoname").autocomplete(
	"${ctx}/goods/ajaxAutoName.action",
	{
	delay:10,
	minChars:1,
	matchSubset:1,
	matchContains:1,
	cacheLength:10,
	onItemSelect:selectItem,
	onFindValue:findValue,
	formatItem:formatItem,
	autoFill:true
}
);
});

</script>
	</head>



  <td><input type="text" id="autoname" class="ac_input" name="memberName"/></td>

java 部分


  public String ajaxAutoName(){
        String keyword=request.getParameter("q");
        List<String> memberList=memberService.getMemberByLike(keyword);
        try {   
                        response.setContentType("text/plain; charset=GBK");   
                        PrintWriter out = response.getWriter();
                        for (String str : memberList){
                            out.println(str);
                        }
   
                        out.flush();   
                        out.close();   
                        return null;   
                    } catch (Exception e) {   
                        e.printStackTrace();   
                       
                   }   
        return null;
    }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。