900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > javascript搜索框联想搜索_js实现输入框联想搜索

javascript搜索框联想搜索_js实现输入框联想搜索

时间:2023-01-27 16:35:10

相关推荐

javascript搜索框联想搜索_js实现输入框联想搜索

//模糊查询

functionSearchLike(jsons) {//var timerSerch = null;

$(document).on('keyup', '[name=' + jsons.name + ']', function() {var This = this;this.timerSerch = setTimeout(function () {//如果一直输入,就先不查询,等键抬起500毫秒之后再进行查询。

getSearchValue({

This: This,

Event:"keyup",

url: jsons.url,

fun: jsons.fun

});

},500);

$(this).parents('.autoSupRelative').find('.autoSupAbsolute').show();

});

$(document).on('keydown', '[name=' + jsons.name + ']', function() {var _this = this;

clearTimeout(_this.timerSerch);

});

$(document).on('click', '.autoSupAbsolute li', function(e) {var name = $(this).parents('.autoSupRelative').find('input[type=text]').attr('name');if (name ==jsons.name) {var thisHtml = $(this).attr('companyName');//.replace('', '');//.split('

//thisHtml = thisHtml.split(',')[0];

if(jsons.HideName) {

$(this).parents('.autoSupRelative').find('[name=' + jsons.HideName + ']').val($(this).attr("PK_Guid"));

}if(thisHtml) {

$(this).parents('.autoSupRelative').find('[name=' + jsons.name + ']').val(thisHtml);

}if(jsons.fun1) {

jsons.fun1(this);

}

$(this).parents('.autoSupRelative').find('[name=' + jsons.name + ']').siblings('ul').html('');

$(this).parents('.autoSupAbsolute').hide();

}

});//$('[name=' + jsons.name + ']').on('click', function (e) {

$(document).on('click', '[name=' + jsons.name + ']', function(e) {

e.stopPropagation();var This = this;

$('.autoSupRelative').css({ 'zIndex': '99'});

$(this).parents('.autoSupRelative').css({ 'zIndex': '100'});

$(this).parents('.autoSupRelative').find('.autoSupAbsolute').show();//if ($(this).parents('.autoSupRelative').find('ul li').length ==0) {//如果没有输入字段,就不重新检索

$(this).parents('.autoSupRelative').find('ul').html('

数据加载,请稍后....');

getSearchValue({

This: This,

Event:"click",

url: jsons.url,

fun: jsons.fun

});//}

}).on("blur", function() {

$('.autoSupRelative').css({ 'zIndex': ''});

});

$(document).on('click', function(e) {

$('.autoSupAbsolute').hide();

});functiongetSearchValue(json) {if(jsons.setUrl) {//jsons.setUrl();

json.url =jsons.setUrl();

}

$.ajaxSetup({ cache:false});

$.ajax(

{

type:"GET",//POST

url: json.url +encodeURIComponent($(json.This).val()),

success:function(msg) {var htmlInit = '';//if (msg != "") {

msg =JSON.stringify(msg)

msg= msg.replace(/'/g, '"');//把单引号替换成双引号

//var datas = jQuery.parseJSON(msg);

var datas =[]

datas=jQuery.parseJSON(msg).result;var html = '';if (json.Event == 'click') {if (datas.length == 0) {

$(json.This).next().val('');//清除隐藏域的赋值

htmlInit = '

未找到此项....';

}else{for (var i = 0; i < datas.length; i++) {if(json.fun) {

htmlInit+=json.fun(datas[i]);

}else{

htmlInit+= '

' + datas[i].companyName + '';

}

}

}

$(json.This).parents('.autoSupRelative').find('.autoSupAbsolute').html(htmlInit);

$(json.This).parents('.autoSupRelative').find('.autoSupAbsolute').show();

}if (json.Event == 'keyup') {if ($(json.This).val() == '') {

$(json.This).parents('.autoSupRelative').find('ul').html('');

$(json.This).parents('.autoSupRelative').find('.autoSupAbsolute').hide();

}else{if (datas.length == 0) {

$(json.This).next().val('');//清除隐藏域的赋值

html = '

未找到此项....';

}else{for (var i = 0; i < datas.length; i++) {if(json.fun) {

html+=json.fun(datas[i]);

}else{

html+= '

' + datas[i].companyName + '';

}

}

}

$(json.This).parents('.autoSupRelative').find('.autoSupAbsolute').html(html);

}

}//}

},

error:function() {

$(json.This).parents('.autoSupRelative').find('ul').html('

数据加载失败....');

}

}

);

}

}//使用--点击输入框,请求接口--url是联想搜索接口,companyName没有值的时候显示所有的列表--下拉列表选择把姓名和id复制给input

SearchLike({

name:'CompanyName', url: '/CompanyCustomBill/CompanySimpInfo?companyName=', fun1: function(_this) {

$("#CompanyName").val($(_this).attr('companyName'));

$("[name=CompanyGuid]").val($(_this).attr('PK_Guid'));

}

});

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。