頁(yè)面版文本框智能提示JS代碼
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>無(wú)標(biāo)題頁(yè)</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();//保存提示框中索引對(duì)應(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 = "";//保存自動(dòng)計(jì)時(shí)對(duì)象
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);//啟用計(jì)時(shí)
}
//獲取對(duì)應(yīng)屬性的長(zhǎ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;
}
//停止計(jì)時(shí)
function fnStopInterval()
{
window.clearInterval(oInterval);
}
//自動(dòng)完成提示
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";
}
//使用鍵盤(pán)上下方向鍵和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屬性,屏蔽了文本框輸入記錄提示功能,雖然這個(gè)功能很好,但是在這里卻成了絆腳石。呵呵
以前沒(méi)有寫(xiě)博客的習(xí)慣,好多不經(jīng)常使用的東西用過(guò)就忘了。以后是要整理整理了。
- 仿百度輸入框智能提示的js代碼
- js文本框輸入內(nèi)容智能提示效果
- jQuery在vs2008及js文件中的無(wú)智能提示的解決方法
- JS實(shí)現(xiàn)仿google、百度搜索框輸入信息智能提示的實(shí)現(xiàn)方法
- VSCode中如何利用d.ts文件進(jìn)行js智能提示
- JS實(shí)現(xiàn)搜索關(guān)鍵詞的智能提示功能
- asp.net 頁(yè)面版文本框智能提示JSCode (升級(jí)版)
- 利用jsonp跨域調(diào)用百度js實(shí)現(xiàn)搜索框智能提示
- 百度搜索框智能提示案例jsonp
- 如何為你的JS項(xiàng)目添加智能提示與類型檢查詳解
相關(guān)文章
javascript代碼在ie8里報(bào)錯(cuò) document.getElementById(...) 為空或不是對(duì)象的解決方
今天更升級(jí)了ie8,發(fā)現(xiàn)原來(lái)在ie7下可以運(yùn)行的代碼,不能運(yùn)行了,發(fā)現(xiàn)了一些細(xì)節(jié),附臨時(shí)修改辦法。2009-11-11基于css3新屬性transform及原生js實(shí)現(xiàn)鼠標(biāo)拖動(dòng)3d立方體旋轉(zhuǎn)
這篇文章主要介紹了基于css3新屬性transform及原生js實(shí)現(xiàn)鼠標(biāo)拖動(dòng)3d立方體旋轉(zhuǎn) 的相關(guān)資料,需要的朋友可以參考下2016-06-06bootstrap實(shí)現(xiàn)嵌套模態(tài)框的實(shí)例代碼
這篇文章主要介紹了bootstrap實(shí)現(xiàn)嵌套模態(tài)框的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01JS實(shí)現(xiàn)判斷數(shù)組是否包含某個(gè)元素示例
這篇文章主要介紹了JS實(shí)現(xiàn)判斷數(shù)組是否包含某個(gè)元素,涉及javascript屬性操作與正則判斷相關(guān)使用技巧,需要的朋友可以參考下2019-05-05javascript原始值和對(duì)象引用實(shí)例分析
這篇文章主要介紹了javascript原始值和對(duì)象引用的方法,實(shí)例分析了javascript原始值和對(duì)象引用的功能、定義與相關(guān)技巧,需要的朋友可以參考下2015-04-04javascript 得到文件后綴名的思路及實(shí)現(xiàn)
在上傳文件時(shí),常常要對(duì)文件的類型即對(duì)文件的后綴名進(jìn)行判斷,用javascript可以很容易的做到這一點(diǎn)。用Javascript解析一個(gè)帶絕對(duì)路徑的文件名并得到后綴名的方法有很多種,這里列出一種,以供參考。2013-07-07js密碼強(qiáng)度實(shí)時(shí)檢測(cè)代碼
這篇文章主要為大家詳細(xì)介紹了js密碼強(qiáng)度實(shí)時(shí)檢測(cè)代碼,密碼強(qiáng)度的判斷, 在注冊(cè)網(wǎng)站用戶的時(shí)候, 是一個(gè)必須要做的事情,如何實(shí)現(xiàn)js密碼強(qiáng)度檢測(cè),感興趣的小伙伴們可以參考一下2016-03-03