原本準備在上一篇中直接修改的,無奈編輯功能太差,開啟一堆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>