程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net 頁面版文本框智能提示JSCode (升級版)

asp.net 頁面版文本框智能提示JSCode (升級版)

編輯:ASP.NET基礎
原本准備在上一篇中直接修改的,無奈編輯功能太差,打開一堆html代碼,空格“ ”都看的人眼花缭亂,只好另開一篇。

升級說明:添加了針對一個界面多個職能提示位置的設定,只需修改文本框onfocus="fnStartInterval(this,'DropDownList2')",

設置好相應的參數即可,同時修復了在IE6下div無法遮蓋下拉列表的問題,(IE6下無論如何設置select的z-index或div的z-index屬性均無濟於事),關於這個就是利用了一個iframe,將其蓋在div要顯示的位置,然後div再放在iframe上方即可。即使下方有select元素,也沒關系了。下面是最新code:

復制代碼 代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AutoTemple.aspx.cs" Inherits="AutoTemple" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>無標題頁</title>
<style type="text/css"><!--
#coverddl{
position:absolute;
z-index:2;
width:expression(this.nextSibling.offsetWidth);
height:expression(this.nextSibling.offsetHeight);
top:expression(this.nextSibling.offsetTop);
left:expression(this.nextSibling.offsetLeft);
}

--></style>
<script type="text/javascript" language="javascript"><!--
var currentIndex=-1;//保存提示框中選擇的索引
var sumSearchCount=0;//保存提示框中數據數量
var tempValue="";//保存當前輸入的要搜索的內容
var objTxt=null;//保存文本框對象
var top=0;//提示框的top
var left=0;//提示框的left
var width=0;//提示框的width
var values = null;//保存下拉列表的值
var texts = null;//保存下拉列表的顯示內容
var tempDiv= null;//保存提示框中索引對應的values索引
var ddlName="";//獲取到的下拉列表ID
var getDDLName = "";//服務器端下拉列表ID
var fontSize=12;//智能提示內容字體
var paddingBottom = 2;//智能提示內容下邊緣大小
var backGroundColor = "#3366CC";//智能提示內容背景色
//獲取下拉列表ID
function GetDDLID()
{
var ddls = document.getElementsByTagName("select");
for(var i=0;i<ddls.length;i++)
{
if(ddls[i].id.indexOf(getDDLName)!=-1)
{
ddlName=ddls[i].id;
break;
}
}
}

//獲取下拉列表的值和顯示內容
function getSelectValues(){
GetDDLID();
values = new Array();
texts = new Array();
tempDiv=new Array();
ddlvalue = document.getElementById(ddlName);
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,ddlOldName){
getDDLName=ddlOldName;
getSelectValues();
objTxt=txt_id;//獲取輸入文本框對象
top = getLength("offsetTop",txt_id.id)+objTxt.offsetHeight;
left= getLength("offsetLeft",txt_id.id);
width=objTxt.offsetWidth-2;
oInterval = window.setInterval("beginSearch()",2000);//啟用計時
}

//獲取對應屬性的長度
function getLength(attribute,id)
{
var offset = 0;
var item = document.getElementById(id);
while (item)
{
offset += item[attribute];
item = item.offsetParent;
}
return offset;
}

//停止計時
function fnStopInterval()
{
window.clearInterval(oInterval);
}

//自動完成提示
function beginSearch(){
if(objTxt.value.length>0 && tempValue!=objTxt.value)
{
sumSearchCount=0;
tempValue=objTxt.value;
var iframe_show = document.getElementById("coverddl");
var div_value = document.getElementById("divMsg");
iframe_show.style.display="block";
div_value.style.top=top+"px";
div_value.style.display="block";
div_value.style.left=left+"px";
div_value.style.width=width+"px";
div_value.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:"+fontSize+"px; display:block; padding-top:2px; padding-bottom:"+paddingBottom+"px; width:100%' id='divsearch_"+i+"' onmouseover=\"this.style.backgroundColor='"+backGroundColor+"';currentIndex="+i+";\" onmouseout=\"this.style.backgroundColor='';currentIndex=-1;\" onclick=\"span_click(this)\" >"+texts[i]+"</div>";
tempDiv[sumSearchCount]=i;
sumSearchCount++;
}
}
div_value.innerHTML=row;
}
else if(objTxt.value.length==0 || objTxt.value == null)
{
document.getElementById("coverddl").style.display="none";
document.getElementById("divMsg").innerHTML="";
}
}

//提示內容單擊保存到文本框中
function span_click(sp)
{
clear();
objTxt.value=sp.innerHTML;
document.getElementById(ddlName).options[sp.id.substring(sp.id.indexOf('_')+1,sp.id.length)].selected="selected";
document.getElementById(ddlName).fireEvent("onchange");
}

//停止查詢,關閉提示
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();
values=null;
texts=null;
tempDiv=null;
currentIndex=-1;
tempValue="";
document.getElementById("coverddl").style.display="none";
document.getElementById("divMsg").style.display="none";
}

//使用鍵盤上下方向鍵和enter鍵
function changeSelect()
{
var iframeContent = document.getElementById("coverddl");
if(iframeContent && iframeContent.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(ddlName).options[tempDiv[currentIndex]].selected="selected";
clear();
//document.getElementById(ddlName).fireEvent("onchange");
//document.form1.onsubmit=function (){return false;};
}
}
}
}
}


// --></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="txtSearch" autocomplete="off" onkeydown="changeSelect()" onfocus="fnStartInterval(this,'DropDownList1')" onblur="closeSearch()" />
<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="slr_realname" DataValueField="systemloginrecord_id" DataSourceID="ObjectDataSource1" Width="130px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList><asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetRecordDS"
TypeName="TestDAL"></asp:ObjectDataSource>
</div>
<iframe id="coverddl" style="position:absolute; z-index:2; display:none;" style="position:absolute; z-index:2; display:none;" >

</iframe>
<div style="z-index:3; display:none; text-align:left; position:absolute; border:solid 1px;" style="z-index:3; display:none; text-align:left; position:absolute; border:solid 1px;" id="divMsg">
</div>
<div>
<input type="text" ID="txtTwo" runat="server" autocomplete="off" onkeydown="changeSelect()" onfocus="fnStartInterval(this,'DropDownList2')" onblur="closeSearch()" /><br />
<asp:DropDownList ID="DropDownList2" DataTextField="Slr_name" DataValueField="Systemloginrecord_id" runat="server" DataSourceID="ObjectDataSource1">
</asp:DropDownList>
</div>
</form>
</body>

</html>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved