频道直达 - 学院 - 下载 - 交易 - 特效 - 字库 - 手册 -排名-工具- 繁體
网页教学网站开发 设为首页
加入收藏
联系我们
建站搜索: 常用广告代码   用户注册 | 用户登陆
您当前的位置:中国建站之家 -> 网站开发设计技术教程 -> asp教程 -> 下拉菜单输入,根据输入内容自动定位

下拉菜单输入,根据输入内容自动定位

作者:未知  来源:转载  发布时间:2005-9-16 0:45:14  发布人:acx

减小字体 增大字体

下拉菜单支持输入,并根据输入内容自动定位:

参考:演员发表于 10/23/2001 8:58:16 AM 的文章 “罗亭的可输入下拉框的解密简化版.”,在此特别感谢相关人等。

本文为这个下拉框增加了一点小小的功能:输入能够定位在已有的选择框内。还有一个缺点,各位给改改:输入的时候不能够自动拉开选择框,怎么办?

function getLeftPostion( theObj )
{
  var pos = 0;
  while ( theObj != null )
  {
    pos += theObj.offsetLeft;
    //get the Object which contain theObj.
    theObj = theObj.offsetParent;
  }
  return pos;
}
function getTopPostion( theObj )
{
  var pos = 0;
  while ( theObj != null )
  {
    pos += theObj.offsetTop;
    //get the Object which contain theObj.
    theObj = theObj.offsetParent;
  }
  return pos;
}
function checkVersion()
{
  var isBadVersion=true;
  var curVer=navigator.appVersion;
  var pos=parseInt(curVer.indexOf("MSIE"));
  if (pos>=1)
  {
    var intVer=parseInt(curVer.charAt(pos+5));
    if (intVer>=5)
    { isBadVersion=false;}
  }
  if (isBadVersion)
  {
    var msg="This page may not be displayed properly:\n"+
            " This product requires Microsoft Internet Explorer 5 or later browser only.";
    alert(msg);
  }
}

//check the browser version
checkVersion();

// the array of comboBoies
theArray = new Array();

function combobox(objId, objHandler)
{
    this.comObj = document.all[objId];
    this.comObj.selectedIndex = -1;
    this.getValue = getValue;
    this.doResize = doResize;
    this.doChange = doChange;
    this.loseFocus = loseFocus;
    this.doSelectIdx = doSelectIdx;
    this.focus = focus;
    this.keyPress = keyPress;
    this.change = change;

    var strMsg="";

//------------------------------------------------------------------------------------------------------
// create the text object
//------------------------------------------------------------------------------------------------------
    var txtObjIdName = objId + "_text";

    if (document.all[txtObjIdName] != null)
    {
        strMsg="The following id: '" + txtObjIdName +"' is used internally by the Combo Box!\r\n"+
           "Use of this id in your page may cause malfunction. Please use another id for your controls.";
        alert(strMsg);
    }

    var txtInner = "<INPUT type='text' id=" + txtObjIdName + " name=" + txtObjIdName + " onblur='" + objHandler + ".loseFocus()' " + " onkeyup='" + objHandler + ".keyPress()'" +  " onchange='" + objHandler + ".keyPress()'" + " style='display: none; position: absolute' value='' >";

    this.comObj.insertAdjacentHTML("afterEnd", txtInner);

    this.txtObj = document.all[txtObjIdName];

//------------------------------------------------------------------------------------------------------
// end
//------------------------------------------------------------------------------------------------------

    this.beResizing = false;
    this.doResize();
    theArray[theArray.length] = this;
}

function loseFocus()
{
    var theComObj = this.comObj;
    var theTxtObj = this.txtObj;
    var i;
    theComObj.selectedIndex = -1;

    if (theTxtObj.value == "")
    {
        return;
    }

    var optLen = theComObj.options.length;
    for (i=0; i<optLen; i++)
    {
        var comVal = theComObj.options[i].text;
        var txtVal = theTxtObj.value;

        if (comVal == txtVal)
        {
            theComObj.selectedIndex = i;
            return;
        }
    }
}

function doResize()
{
    if (!this.beResizing)
    {
        this.beResizing = true;
        this.txtObj.style.display="none";
        this.comObj.style.position="static";
        this.txtObj.style.posLeft = getLeftPostion(this.comObj);
        this.txtObj.style.posTop = getTopPostion(this.comObj) + 1;
        this.txtObj.style.posWidth = this.comObj.offsetWidth - 16;
        this.txtObj.style.posHeight = this.comObj.offsetHeight;
        this.comObj.style.position ="absolute";
        this.comObj.style.posLeft = this.txtObj.style.posLeft;
        this.comObj.style.posTop = this.txtObj.style.posTop;
        this.offWidth = this.comObj.offsetWidth;
    
        var strRect = "rect(0 "+(this.comObj.offsetWidth)+" "+ this.comObj.offsetHeight + " "+(this.txtObj.style.posWidth - 2 )+")";

        this.comObj.style.clip = strRect;
        this.txtObj.style.display="";
        this.beResizing = false;
    }
}

function doChange()
{
    var idx = this.comObj.selectedIndex;
    var opt = this.comObj.options[idx];
    this.txtObj.value = opt.text;
    this.txtObj.focus();
    this.txtObj.select();
    this.comObj.selectedIndex=-1;
}

function getValue()
{
    return this.txtObj.value;
}

function doSelectIdx(i)
{
    var optLen = this.comObj.options.length;

    if ((i >=0) && (i < optLen))
    {
        this.comObj.selectedIndex = i;
        this.txtObj.value = this.comObj.options[i].text;
        return;
    }
    this.txtObj.value = "";
}


function focus()
{
    this.txtObj.focus();
}

/*resize all combobox when window be resized*/
function resetAllSize()
{
    var i;
    for (i=0; i < theArray.length; i++)
    {
        theArray[i].doResize();
    }
}
////////////////定位函数开始,我加的///////////////
function keyPress()
{
    var txtStr;
    var comStr;
    var maxInt = 0;
    var defInt = 0;
    var defJ;
    txtStr = this.txtObj.value;
    //document.form1.test.value=txtStr;
    var j;
    for(j=0;j<this.comObj.options.length;j++)
    {
        comStr = this.comObj.options[j].text;
        var m;
        for(m=0;m<txtStr.length+1;m++)
        {
            if(txtStr.charCodeAt(m) != comStr.charCodeAt(m))
            {
                maxInt = m;
                break;
            }
        }
        if (defInt < maxInt)
        {
            defInt = maxInt;
            defJ = j;
        }
    }
    this.comObj.selectedIndex = defJ;
}

将本文收藏到QQ书签与更多好友分享
[打 印]
[] [返回上一页] [收 藏]
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论...]
精彩推荐
热门文章
· 注册码大全二
· 注册码大全四
· 注册码大全一
· 要10G免费网络硬盘的请进..
· 通过google 赶快来赚美金..
· 注册码大全十
· 头像-qq头像(qq新头像)4..
· 让你轻松架设FTP服务器1..
· 注册码大全三
· 梦幻背景图片7
· 卡通动物图片6
· 网页制作素材-按钮素材2..
· 让你轻松架设FTP服务器5..
· 风景图片8
· 注册码大全九
· 让你轻松架设FTP服务器2..
关注此文读者还看过
· C#中ArrayList.CopyTo()..
· Dreamweaver构建Blog全程..
· 用.net静态变量取代Appl..
· Webmail攻防实战(4)
· 本地分类信息网站线下推..
· 如何用PHP发电子邮件(2)..
· 消息称Google拟收购博客..
· ASP.NET实现网络空间管理..
· MySQL安全性指南 (1)
· XML在.net平台下的自定义..
· ASP.NET虚拟主机的重大安..
· Access数据库技术(67)
· 用 Photoshop 做杯中冰块..
· 2005年最佳Web2.0软件大..
· 黑龙江阿城市的一对夫妇..
· ASP基础教材IIII
相关文章
· 用JSP+JavaScript打造二级级..
· 用Javascript判断下拉菜单的..
· 美工版(不用写脚本的) IE,f..
· BOX 文字底色快捷下拉菜单D..
· 三层动态下拉菜单的例子
· 下拉菜单全攻略之Dre..
· 用Dreamweaver MX制作..
· 利用Dreamweaver的插..
· 动态创建下拉菜单
· 不刷新页面改变下拉菜单内容..
· 用DHTML来模拟实现下拉菜单..
· 介绍一个制作下拉菜单完全不..
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 人才招聘
网站合作、内容监督、商务咨询:QQ: 9576619
Copyright ? 2005--2008 中国建站之家版权所有
粤ICP备05092265号