手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >页面版文本框智能提示JS代码
页面版文本框智能提示JS代码
摘要:于是这code便诞生了,如下:复制代码代码如下:无标题页varcurrentIndex=-1;//保存提示框中选择的索引varsumSear...

于是这code便诞生了,如下:

复制代码 代码如下:

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>无标题页</title>

<script type="text/javascript" language="javascript">

var currentIndex=-1;//保存提示框中选择的索引

var sumSearchCount=0;//保存提示框中数据数量

var tempValue="";//保存当前输入的要搜索的内容

var objTxt="";//保存文本框对象

var top=0;//提示框的top

var left=0;//提示框的left

var width=0;//提示框的width

var values = new Array();//保存下拉列表的值

var texts = new Array();//保存下拉列表的显示内容

var tempDiv=new Array();//保存提示框中索引对应的values索引

//获取下拉列表的值和显示内容

function getSelectValues(ddl){

ddlvalue = document.getElementById("DropDownList1");

for(var i=0;i<ddlvalue.length;i++){

values[i]=ddlvalue.options[i].value;

texts[i]=ddlvalue.options[i].text;

}

}

var oInterval = "";//保存自动计时对象

function fnStartInterval(txt_id){

getSelectValues("DropDownList1");

objTxt=txt_id;//获取输入文本框对象

top = getLength("offsetTop")+objTxt.offsetHeight;

left= getLength("offsetLeft");

width=objTxt.offsetWidth;

oInterval = window.setInterval("beginSearch()",2000);//启用计时

}

//获取对应属性的长度

function getLength(attribute)

{

var offset = 0;

var txt_input = document.getElementById("txtSearch");

while (item)

{

offset += txt_input[attribute];

txt_input = txt_input.offsetParent;

}

return offset;

}

//停止计时

function fnStopInterval()

{

window.clearInterval(oInterval);

}

//自动完成提示

function beginSearch(){

if(objTxt.value.length>0 && tempValue!=objTxt.value)

{

sumSearchCount=0;

tempValue=objTxt.value;

var div_show = document.getElementById("divMsg");

div_show.style.top=top+"px";

div_show.style.display="block";

div_show.style.left=left+"px";

div_show.style.width=width+"px";

div_show.innerHTML="";

var leng = texts.length;

var txt_value = objTxt.value;

var row="";

for(var i=0;i<leng;i++){

if(texts[i].indexOf(txt_value)!=-1){

row = row + "<div id='divsearch_"+i+"' onmouseover="this.style.backgroundColor='#3366CC';currentIndex="+i+";" onmouseout="this.style.backgroundColor='';currentIndex=-1;" onclick="span_click(this)" >"+texts[i]+"</div>";

tempDiv[sumSearchCount]=i;

sumSearchCount++;

}

}

div_show.innerHTML=row;

}

else if(objTxt.value.length==0 || objTxt.value == null)

{

var div_msg = document.getElementById("divMsg");

div_msg.style.display="none";

div_msg.innerHTML="";

}

}

//提示内容单击保存到文本框中

function span_click(sp)

{

clear();

objTxt.value=sp.innerHTML;

document.getElementById("DropDownList1").options[sp.id.substring(sp.id.indexOf('_')+1,sp.id.length)].selected="selected";

}

//停止查询,关闭提示

function closeSearch()

{

var tbl = document.activeElement.parentElement;

if(tbl && tbl.id!="divMsg")//防止使用上下键后丢失提示内容

{

clear();

document.getElementById("divMsg").innerHTML="";

}

else if(currentIndex==-1)

{

clear();

document.getElementById("divMsg").innerHTML="";

}

}

//清空提示

function clear()

{

fnStopInterval();

currentIndex=-1;

tempValue="";

document.getElementById("divMsg").style.display="none";

}

//使用键盘上下方向键和enter键

function changeSelect()

{

var divContent = document.getElementById("divMsg");

if(divContent && divContent.style.display=="block")

{

if (event.keyCode == 38 || event.keyCode == 40 || event.keyCode == 13)

{

if(currentIndex!=-1) document.getElementById("divsearch_"+tempDiv[currentIndex]).style.backgroundColor="";

if (event.keyCode == 38 && currentIndex > 0)

{

currentIndex--;

document.getElementById("divsearch_"+tempDiv[currentIndex]).style.backgroundColor="#3366CC";

}

else if (event.keyCode == 40 && currentIndex < sumSearchCount-1)

{

currentIndex++;

document.getElementById("divsearch_"+tempDiv[currentIndex]).style.backgroundColor="#3366CC";

}

else if (event.keyCode == 13)

{

if(currentIndex > -1)

{

var divpart = document.getElementById("divsearch_"+tempDiv[currentIndex]);

objTxt.value=divpart.innerHTML;

document.getElementById("DropDownList1").options[tempDiv[currentIndex]].selected="selected";

clear();

}

}

}

}

}

</script>

</head>

<body>

<form id="form1" runat="server">

<div>

<input type="text" id="txtSearch" autocomplete="off" onkeydown="changeSelect()" onfocus="fnStartInterval(this)" onblur="closeSearch()" runat="server" />

<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="slr_realname" DataValueField="systemloginrecord_id" DataSourceID="ObjectDataSource1" Width="130px">

</asp:DropDownList><asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetRecordDS"

TypeName="TestDAL"></asp:ObjectDataSource>

</div>

<div id="divMsg">

</div>

</form>

</body>

</html>

<input type="text" id="txtSearch" autocomplete="off"。。。这里加入了autocomplete属性,屏蔽了文本框输入记录提示功能,虽然这个功能很好,但是在这里却成了绊脚石。呵呵

以前没有写博客的习惯,好多不经常使用的东西用过就忘了。以后是要整理整理了。

【页面版文本框智能提示JS代码】相关文章:

jQuery计算文本框字数及限制文本框字数的方法

必须点击广告才能进入的代码

经验几则 推荐

EasyUI闪屏EasyUI页面加载提示(原理+代码+效果图)

JavaScript实现将UPC转换成ISBN的方法

获得当前页面URL地址的三个JS代码

Javascript特效:随机显示图片的源代码

免费空间广告万能消除代码

Javascript随机显示图片的源代码

JQuery中DOM实现事件移除的方法

精品推荐
分类导航