頁面版文本框智能提示JS代碼
更新時間:2009年11月20日 17:59:39 作者:
首先說下背景,該code用于一個多條件查詢界面,原本該查詢條件由一個下拉列表提供,但是由于下拉列表數(shù)據(jù)量過大,用戶使用不方便,便希望在頁面給出一個智能提示的功能,但搜索的數(shù)據(jù)來自下拉列表
于是這code便誕生了,如下:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>無標(biāo)題頁</title>
<script type="text/javascript" language="javascript">
var currentIndex=-1;//保存提示框中選擇的索引
var sumSearchCount=0;//保存提示框中數(shù)據(jù)數(shù)量
var tempValue="";//保存當(dāng)前輸入的要搜索的內(nèi)容
var objTxt="";//保存文本框?qū)ο?
var top=0;//提示框的top
var left=0;//提示框的left
var width=0;//提示框的width
var values = new Array();//保存下拉列表的值
var texts = new Array();//保存下拉列表的顯示內(nèi)容
var tempDiv=new Array();//保存提示框中索引對應(yīng)的values索引
//獲取下拉列表的值和顯示內(nèi)容
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;//獲取輸入文本框?qū)ο?
top = getLength("offsetTop")+objTxt.offsetHeight;
left= getLength("offsetLeft");
width=objTxt.offsetWidth;
oInterval = window.setInterval("beginSearch()",2000);//啟用計時
}
//獲取對應(yīng)屬性的長度
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 style=\"font-size:14px; display:block; width:100%\" 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="";
}
}
//提示內(nèi)容單擊保存到文本框中
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";
}
//停止查詢,關(guān)閉提示
function closeSearch()
{
var tbl = document.activeElement.parentElement;
if(tbl && tbl.id!="divMsg")//防止使用上下鍵后丟失提示內(nèi)容
{
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 style="display:none; z-index:2; text-align:left; position:absolute; border:solid 1px;" id="divMsg">
</div>
</form>
</body>
</html>
<input type="text" id="txtSearch" autocomplete="off"。。。這里加入了autocomplete屬性,屏蔽了文本框輸入記錄提示功能,雖然這個功能很好,但是在這里卻成了絆腳石。呵呵
以前沒有寫博客的習(xí)慣,好多不經(jīng)常使用的東西用過就忘了。以后是要整理整理了。
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>無標(biāo)題頁</title>
<script type="text/javascript" language="javascript">
var currentIndex=-1;//保存提示框中選擇的索引
var sumSearchCount=0;//保存提示框中數(shù)據(jù)數(shù)量
var tempValue="";//保存當(dāng)前輸入的要搜索的內(nèi)容
var objTxt="";//保存文本框?qū)ο?
var top=0;//提示框的top
var left=0;//提示框的left
var width=0;//提示框的width
var values = new Array();//保存下拉列表的值
var texts = new Array();//保存下拉列表的顯示內(nèi)容
var tempDiv=new Array();//保存提示框中索引對應(yīng)的values索引
//獲取下拉列表的值和顯示內(nèi)容
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;//獲取輸入文本框?qū)ο?
top = getLength("offsetTop")+objTxt.offsetHeight;
left= getLength("offsetLeft");
width=objTxt.offsetWidth;
oInterval = window.setInterval("beginSearch()",2000);//啟用計時
}
//獲取對應(yīng)屬性的長度
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 style=\"font-size:14px; display:block; width:100%\" 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="";
}
}
//提示內(nèi)容單擊保存到文本框中
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";
}
//停止查詢,關(guān)閉提示
function closeSearch()
{
var tbl = document.activeElement.parentElement;
if(tbl && tbl.id!="divMsg")//防止使用上下鍵后丟失提示內(nèi)容
{
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 style="display:none; z-index:2; text-align:left; position:absolute; border:solid 1px;" id="divMsg">
</div>
</form>
</body>
</html>
<input type="text" id="txtSearch" autocomplete="off"。。。這里加入了autocomplete屬性,屏蔽了文本框輸入記錄提示功能,雖然這個功能很好,但是在這里卻成了絆腳石。呵呵
以前沒有寫博客的習(xí)慣,好多不經(jīng)常使用的東西用過就忘了。以后是要整理整理了。
相關(guān)文章
javascript代碼在ie8里報錯 document.getElementById(...) 為空或不是對象的解決方
今天更升級了ie8,發(fā)現(xiàn)原來在ie7下可以運行的代碼,不能運行了,發(fā)現(xiàn)了一些細(xì)節(jié),附臨時修改辦法。2009-11-11基于css3新屬性transform及原生js實現(xiàn)鼠標(biāo)拖動3d立方體旋轉(zhuǎn)
這篇文章主要介紹了基于css3新屬性transform及原生js實現(xiàn)鼠標(biāo)拖動3d立方體旋轉(zhuǎn) 的相關(guān)資料,需要的朋友可以參考下2016-06-06bootstrap實現(xiàn)嵌套模態(tài)框的實例代碼
這篇文章主要介紹了bootstrap實現(xiàn)嵌套模態(tài)框的實例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01javascript 得到文件后綴名的思路及實現(xiàn)
在上傳文件時,常常要對文件的類型即對文件的后綴名進(jìn)行判斷,用javascript可以很容易的做到這一點。用Javascript解析一個帶絕對路徑的文件名并得到后綴名的方法有很多種,這里列出一種,以供參考。2013-07-07