日期:2014-05-16  浏览次数:20340 次

JSP动态增加一行,并实现 jquery autocomplete效果,怎么办?
newRowCell2.innerHTML='<input type="hidden" id="shoeId'+index+'" name="shoeId"><input type="hidden" id="remainingId'+index+'" name="remainingId"><input type="text" style="width:100%;" id="productId'+index+'" name="productId" onchange="autoCompleteByProductId('+index+')">'+ '</input>';//这里是动态生成tr的局部代码。
//生成的input框动态调用下面这个方法。

function autoCompleteByProductId(index){
var productId="#productId"+index;
alert($(productId).val());
alert(1);
$(productId).autocomplete('${ctx}/autocomplete/autoCompleteByProductId?timeStamp='+new Date().getTime(),{
max:20,
width:180,
matchSubset:false,
matchContains:true,
dataType:"json",
parse:function(data){
alert();
var rows=[];
for(var i=0;i<data.length;i++){
rows[rows.length]={data:data[i]};
}
return rows;
},
formatItem: function(row, i, total) {  
return "<span>"+row.producttype+" "+row.productId+" "+row.color+"</span>";  
},  
formatMatch: function(row, i, total) {  
return row.productId;  
},  
formatResult: function(row) {  
return row.productId;  
}
});

alert(2);
$(productId).result(onRecordSelect);
function onRecordSelect(event,data,formatted){
if(data.productId!=undefined){
$(productId).val(data.productId);
}
}

}
//但是无法请求后台,autocomplete效果,没有出现,以下是后台代码
public ActionForward autoCompleteByProductId(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
System.err.println("======method:autoCompleteByProductId======"); 
String productId = ParamUtil.getParam(request, "q", "");
List<AutoCompleteByProductId> shoes = shoeManagerImpl.autoCompleteByProductId(productId);
DebugUtil.showAllPorperty(shoes);
int count = 0;
JSONArray jsonArray = new JSONArray();
for (AutoCompleteByProductId shoe : shoes) {
if (count > 10) {
break;
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("productId", shoe.getProductId());
jsonObject.put("producttype", shoe.getProducttype());
jsonObject.put("color", shoe.getColor());
jsonArray.put(jsonObject);
count++;
}
renderText(response, jsonArray.toString());
System.out.println("jsonArray:===" + jsonArray.toString());
return null;
}


------解决方案--------------------
楼主解决没?!