jquery实现仿select列表的即时搜索及拼音搜索

这里提到select,其实不是select,而是用<li><input>标签去仿造一个select,以实现对已有“option”的快速检索功能。

以<input>标签代替select的选择框,以<li>标签代替option标签。每个li标签附onclick触发js带参数的选取事件,所带参数即为option的value.<li>内附带显示text用的input标签,和隐藏的text的拼音input标签。思路就是这些。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>qqqun.21.777.12</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<style type="text/css">
/*拼音检索功能全部css*/
#divselect{	margin:0;
			position:relative;
			z-index:10000;
			background: #fff;
			text-aling:left;}
#divselect ul{margin:0; padding:0}
#divselect ul li{margin:0;
				 height:22px;
				 line-height:22px;
				 border-bottom:1px solid grey;
				 border-left:1px solid grey;
				 border-right:1px solid grey;
				 padding-left:5px;
				 display: none;
				 cursor: pointer;
				 width: 300px;}
#divselect ul li input{cursor: pointer;}
#divselect input{height:22px;
				 line-height:22px;
				 padding:0px;}
</style>
<script>
///拼音检索下拉列表的全部函数,开头必须引用jquery
function showSoSo(){//显示搜索栏
	$('#soso').show();
	document.getElementById('soso').focus();
	$('#xbutton').show();
}
function hideSoSo(){//隐藏搜索栏
	$('#soso').hide();
	$('#xbutton').hide();
	$('#selectUl > li').hide();
}
function inputValue(value,text){//选中option
	$('#formName').attr('value',value);
	$('#seetext').attr('value',text);
	$('#sosoName').attr('value',text);
	$('#soso').attr('value',text);
	hideSoSo();
}
function soIt(){//搜索option列表

	var so = $("#soso").val();
	
	if(so.length>0){
		var kehuhz = document.getElementsByName('hzIndexs');
		var kehupy = document.getElementsByName('pyIndexs');
		var lis = document.getElementById('selectUl').getElementsByTagName('li'); 
	
		for(var c=0;c<kehuhz.length;c++){
			if(kehuhz[c].value.indexOf(so)>-1||kehupy[c].value.indexOf(so)>-1){
				lis[c].style.display='block';
			}else{
				lis[c].style.display='none';
			}
		}
	}
}
</script>
</head>
<body>

<div id='divselect'>
	<input type="text" id='seetext' style="width: 300px;" readonly="readonly" οnclick="showSoSo()"/>
	<input id='soso' type="text" style="width: 300px;margin-left:-306px;display: none;" οnkeyup="soIt()"/>
	<input type="button" id='xbutton' value="x" class='button3' style="display: none;" οnclick='hideSoSo()'>
	<ul id="selectUl">
			<li οnclick="inputValue(1,'选项一')">
				<input name='hzIndexs' value='选项一' type="text" style="width:100%;border: 0px;" readonly="readonly"/>
				<input name='pyIndexs' value='xuanxiangyi' type="hidden" readonly="readonly"/>
			</li>
			<li οnclick="inputValue(2,'选项二')">
				<input name='hzIndexs' value='选项二' type="text" style="width:100%;border: 0px;" readonly="readonly"/>
				<input name='pyIndexs' value='xuanxianger' type="hidden" readonly="readonly"/>
			</li>
			<li οnclick="inputValue(3,'北京大学')">
				<input name='hzIndexs' value='北京大学' type="text" style="width:100%;border: 0px;" readonly="readonly"/>
				<input name='pyIndexs' value='beijingdaxue' type="hidden" readonly="readonly"/>
			</li>
			<li οnclick="inputValue(4,'清华大学')">
				<input name='hzIndexs' value='清华大学' type="text" style="width:100%;border: 0px;" readonly="readonly"/>
				<input name='pyIndexs' value='qinghuadaxue' type="hidden" readonly="readonly"/>
			</li>
	</ul>
</div>
<!--实际提交表单时用的隐藏域  测试时可写成type=text显示以供测试-->
<input type="hidden" name='formName' id='formName' readonly="readonly"/>
</body>
</html>
至于拼音的生成肯定不是手打的,一般option列表都是后台传过来的list用jstl标签去迭代,后台可以用pinyin4j.jar包去生成拼音


  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值