日期:2011-02-27  浏览次数:20930 次

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

参考:演员发表于 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
//------------------------------------------------------------