1.在控件的onfocus绑定,些方法绑定,如果多次点击,会绑定多次autocomplete,查询时会执行多次查询。
<input type="text" name="FEE_NM" class="text_input" style="width: 100%;" value="" onfocus="autoFeetyp(this,150)"/>
<input type="text" name="FEE_TYP" value="" />
/**
* 给控件注册费用类型
* @param obj
* @param width
* @param callBack
*/
function autoFeetyp(obj,width, callBack){
var url = Systempath+"/AutoSuggestServlet?type=feetype&filter=ocode ='OTHERS'&match=true&time=" + Math.round(Math.random() * 10000);
url = encodeURI(encodeURI(url));
$(obj).autocomplete(url, {
multiple : false,
autoFill : false,
mustMatch : false,
matchContains : true,
dataType : 'json',
parse : parseFn,
width : width,
scroll : true,
scrollHeight : 200,
cacheLength : 1000,
matchSubset : true,//只有cacheLength>1时有效
formatItem : function(row, i, max) {
return row.cname;
}
}).result(function(event, item, formatted) {//把返回的结果内容显示在其他文本框上
$(this).val(item.cname);
$(this).next().val(item.code);
if (typeof(callBack) == "function") {
callBack (this);
}
});
}
2.每次绑定autocomplete时,先删除掉原来的input,再重新增加一个新的input,这样可避免在同一个input上绑定多次autocomplete的问题.
//动态绑定jquery.autocomplete
function nature_onchange(obj) {
//动态获取过滤条件
var filter = "";
var nature = $("#nature").val();
if (nature){
filter = "nature='"+nature+"'";
}
//先删除掉原来的input,再重新增加一个新的input,这样可避免在同一个input上绑定多次autocomplete的问题
var portLoad = "<input type=\"text\" onpropertychange=\"writeValue(this,'portLoad');\" id=\"shippingline1\" name=\"shippingline1\" class=\"text_input\" style=\"width: 90%;\"/>"
+"<input type=\"text\" id=\"portLoad\" name=\"portLoad\" />";
$("#shippingline1_td").html(portLoad);
autoSuggest($("#shippingline1"),150,null,"shippingline1",filter);
var portUnload = "<input type=\"text\" onpropertychange=\"writeValue(this,'portUnload');\" id=\"shippingline2\" name=\"shippingline2\" class=\"text_input\" style=\"width: 90%;\"/>"
+"<input type=\"text\" id=\"portUnload\" name=\"portUnload\" />";
$("#shippingline2_td").html(portUnload);
autoSuggest($("#shippingline2"),150,null,"shippingline2",filter);
}
/**
* 给控件注册模糊查询
* @param obj
* @param width
* @param callBack
*/
function autoSuggest(obj,width, callBack,type,filter){
var url = Systempath+"/AutoSuggestServlet?type="+type+"&filter="+filter+"&match=true&time=" + Math.round(Math.random() * 10000);
url = encodeURI(encodeURI(url));
$(obj).autocomplete(url, {
multiple : false,
autoFill : false,
mustMatch : false,
matchContains : true,
dataType : 'json',
parse : parseFn,
width : width,
scroll : true,
scrollHeight : 200,
cacheLength : 1,
matchSubset : false,//只有cacheLength>1时有效
formatItem : function(row, i, max) {
return row.cname;
}
}).result(function(event, item, formatted) {//把返回的结果内容显示在其他文本框上
$(this).val(item.cname);
$(this).next().val(item.code);
if (typeof(callBack) == "function") {
callBack (this);
}
});
}