您的位置:首页 > Web前端 > JQuery

jquery autocomplete ajax 传参数

2015-12-08 10:44 711 查看
必须引入的js  

<link rel="stylesheet" href="/activities/css/jquery-ui.min.css">
<script src="/activities/js/jquery-ui.min.js"></script>
<script src="/activities/js/jquery-1.11.0.min.js"></script>


HTML

<input name="brandName" type="text" class="form-control" id="brandMsg">


js代码
<script>
$(function(){
var banddata=[];

$( "#brandMsg" ).autocomplete({
source: function( request, response ) {
var datas=$("#brandMsg").val();
$.ajax({
type: "get",
url: '/activities/activity/getBrandMessage?brandName='+datas,
dataType: "json",
async: false,
success: function( data ) {
banddata=data.brandResult;
}
});
response( $.map( $.parseJSON(banddata), function( item ) { // 此处是将返回数据转换为 JSON对象,并给每个下拉项补充对应参数
return {
// 设置item信息
label: item, // 下拉项显示内容
value: item,   // 下拉项对应数值
}
}));
},
minLength: 2,  // 输入框字符个等于2时开始查询
select: function( event, ui ) { // 选中某项时执行的操作
// 存放选中选项的信息

}
});
});
</script>


struts2

<action name="getBrandMessage" class="com.xx.emidas.activity.activity.AddActivityAction" method="getbrandMessage">
<result type="json">
<param name="callbackParameter">callback</param>
<param name="excludeProperties">callback</param>
</result>
</action>


关键代码 

public String getbrandMessage(){
try {
brand=dmpService.getBrandInfoByBrandName(brandName);
String[] temp = new String[1];
if(null !=brand){
temp[0]=brand.getName();
}

// String[] temp = "align,both,card,dream,error,fail,gson,hello,invaid,job,kill".split(",");
brandResult = "[";
brandResult+="\"无\",";
if(temp[0]!=null){
for(int i=0;i<temp.length;i++){
if(temp[i].indexOf(temp[i])!=-1){
brandResult += "\""+temp[i]+"\",";
}
}
}
if(brandResult.length() > 1){
brandResult = brandResult.substring(0,brandResult.length()-1);
brandResult+="]";
}
/*		        System.out.println(name);*/

HttpServletResponse res = ServletActionContext.getResponse();
/*  PrintWriter pw = res.getWriter();
pw.write(brandResult);
pw.flush();
pw.close();*/

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return SUCCESS;
}


注:你所需要的数据是 数组格式
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息