需求:用户在输入两个字及以上时,输入框能弹出下拉选择框给用户进行选择。并且若数据库不存在该条数据,用户可输入其他数据进行添加。
源码:
$(function(){
$("#hospitalName").autocomplete({
minChars: 2,
max: 100,
scrollHeight: 300,
scroll: true,
source : function( request, response ) {
var hospitalNameV = request.term.replace(/[a-zA-Z ]/g,"");
var checkFlag = hospitalNameV.length >= 2 && bufferHospitalName != hospitalNameV;
if(checkFlag){
hospitals = [];
$.ajax( {
url: "${ctx}hospital/fizzyQueryHospitalName.do",
dataType: "json",
data: {
hospitalName: hospitalNameV
},
success: function(data){
var dataLength = data.length;
for(var i = 0 ; i < dataLength ; i++){
hospitals.push(data[i].hospitalNameAndCiiCode);
}
response(hospitals);
}} );}
},
select : function(event,ui){
var selectVal = ui.item.value;
var wz = 0;
wz = selectVal.indexOf("(");
var aa = selectVal.substring(0,wz);
var bb = selectVal.substring(wz+1,selectVal.length-1);
$("#hospitalName").val(aa);
setTimeout(function(){
$("#ciiHospitalCode").val(bb);
},100);
return false;
}
});
});
采用input框的autocomplete。source为用户历史输入数据,autocomplete会弹出该框给用户进行选择,select为选择触发的事件。
将ajax放在source里,能够实时返回数据进行选择。
而将autocomplete框放在ajax中的话,显示的为上一次请求的返回结果。
需求:模糊查询选择框,从后台查询数据,用户在输入框中输入后模糊匹配进行选择,若不存在则置空
<sfform:select path="singleClaimInfo.mildDiseaseCode" name="mildDiseaseCode"
id="mildDiseaseCode" class="easyui-combobox">
<option value="">请选择</option>
<sfform:options items="${mildDiseaseList}"
itemLabel="mildDiseaseName" itemValue="mildDiseaseCode" />
</sfform:select>