打字时文本框失去焦点

Textbox lost focus while typing

我正在开发 Web 应用程序,运行 遇到文本框在键入时失去焦点的问题。

具体来说,让我解释一下这是怎么发生的:

  1. 用户点击文本框并开始输入。
  2. 一旦用户在文本框中键入第一个字母,它就会失去焦点。
  3. 要继续输入,用户必须在文本框中再次单击。

注意:文本框是可编辑的下拉列表。

下面是用于下拉的代码。

<input type="text" id="placeDeathState" name="placeDeathState" size="3" maxlength="25" 
    value="" style="vertical-align:top; margin:2px 0px 0px 0px; border:0px;" 
    onfocus="focusAutoSelect(document.getElementById('placeDeathState'), 2, '', '', '');" 
    onblur="this.autoSelect.hideComboList();">

更新一: 函数 focusAutoSelect()

的代码
function focusAutoSelect(obj, col, tgts, cols, scripts) {
    if (!obj.autoSelect)
        obj.autoSelect = new AutoSelectCombo(obj.id, col, tgts, cols, scripts);
}

更新 2:

AutoSelectCombo 对象:

这是它的作用:

这是 AutoSelectCombo 对象。虽然它开始是一个 linear autoselect,现在通过过滤变成 select。任何事物 在此框中输入的内容将用于过滤选项列表 仅包含键入的文本的项目。

function AutoSelectCombo(id, col, tgts, cols, scrs) {
    this.editFocused= false;
    this.selFocused = false;
    this.timer      = null;
    this.column     = col;
    this.quicksel   = false;
    this.quickstr   = "";
    this.targets    = new Array();
    this.columns    = new Array();

    this.scripts    = scrs.split("#");
    this.gotChar    = false;
    this.ascDiv     = document.getElementById(id+"div");
    this.ascText    = document.getElementById(id);
    this.ascImage   = document.getElementById(id+"btn");
    this.ascEditBtn = document.getElementById(id+"edit");
    this.ascSDiv    = document.getElementById(id+"combo");
    this.ascSelect  = document.getElementById(id+"select");
    this.ascFrm     = document.getElementById(id+"ifrm");
    this.options    = new Array();

    this.lastLen    = this.ascText.value.length;
    this.maxItems   = 10;
    this.blurClosed = false;
    this.selIndex=0;

    if ((tgts!=null) && (tgts!="")) {
        this.targets    = tgts.split("|");
        this.columns    = cols.split("|");
    }

    if (this.ascText.readOnly == true)
        this.quicksel = true;

    for (; this.selIndex<this.ascSelect.length; this.selIndex++)
        if (this.ascSelect.options[this.selIndex].value == this.ascSelect.value)
            break;

    if (this.selIndex >= this.ascSelect.length)
        this.selIndex = -1;

    if (this.ascDiv)
        this.ascDiv.autoSelect = this;
    if (this.ascText)
        this.ascText.autoSelect = this;
    if (this.ascImage)
        this.ascImage.autoSelect = this;
    if (this.ascEditBtn)
        this.ascEditBtn.autoSelect = this;
    if (this.ascSDiv)
        this.ascSDiv.autoSelect = this;
    if (this.ascSelect)
        this.ascSelect.autoSelect = this;

    this.ascText.oldOnFocus = this.ascText.onfocus;
    this.ascText.onfocus = function(evt) {
        if (!evt) 
            evt = window.event;

        this.autoSelect.editFocused = true;

        if (this.autoSelect.ascText.oldOnFocus)
            this.autoSelect.ascText.oldOnFocus.call(this.autoSelect.ascText);
    }

    this.ascText.oldOnBlur = this.ascText.onblur;
    this.ascText.onblur = function(evt) {
        if (!evt) 
            evt = window.event;

        this.autoSelect.editFocused = false;
        this.autoSelect.handleBlur(evt);

        if (this.autoSelect.ascText.oldOnBlur)
            this.autoSelect.ascText.oldOnBlur.call(this.autoSelect.ascText);
    }

    this.ascText.oldOnKeyDown = this.ascText.onkeydown;
    this.ascText.onkeydown = function(evt) {
        if (!evt) 
            evt = window.event;

        this.autoSelect.handleComboKeyDown(evt);

        if (this.autoSelect.ascText.oldOnKeyDown)
            this.autoSelect.ascText.oldOnKeyDown.call(this.autoSelect.ascText);
    }

    this.ascText.oldOnKeyPress = this.ascText.onkeypress;
    this.ascText.onkeypress = function(evt) {
        if (!evt) 
            evt = window.event;

        this.autoSelect.handleComboKeyPress(evt);

        if (this.autoSelect.ascText.oldOnKeyPress)
            this.autoSelect.ascText.oldOnKeyPress.call(this.autoSelect.ascText);
    }

    this.ascText.oldOnKeyUp = this.ascText.onkeyup;
    this.ascText.onkeyup = function(evt) {
        if (!evt) {
            evt = window.event;
        }

        this.autoSelect.handleComboKeyUp(evt);

        if (this.autoSelect.ascText.oldOnKeyUp) {
            this.autoSelect.ascText.oldOnKeyUp.call(this.autoSelect.ascText);
        }
    }

    this.ascImage.oldOnClick = this.ascImage.onclick;
    this.ascImage.onclick = function(evt) {
        if (!evt) {
            evt = window.event;
        }

        this.autoSelect.editFocused = true;
        this.autoSelect.toggleComboList();
        this.autoSelect.blurClosed = false;

        //if (this.autoSelect.ascImage.oldOnClick)
        //  this.autoSelect.ascImage.oldOnClick.call(this.autoSelect.ascImage);
    }

    this.ascSelect.oldOnKeyPress = this.ascSelect.onkeypress;
    this.ascSelect.onkeypress = function(evt) {
        if (!evt) 
            evt = window.event;

        var keyCode = evt.keyCode ? evt.keyCode : evt.which ? evt.which : evt.charCode;
        if (evt.keyCode == 13) {
            this.autoSelect.hideComboList();
            return false;
        }
    }

    this.ascSelect.oldOnChange = this.ascSelect.onchange;
    this.ascSelect.onchange = function(evt) {
        if (!evt) 
            evt = window.event;

        this.autoSelect.setComboValue();
//      this.autoSelect.hideComboList();

        //if (this.autoSelect.ascSelect.oldOnChange)
        //  this.autoSelect.ascSelect.oldOnChange.call(this.autoSelect.ascSelect);
    }

    this.ascSelect.oldOnClick = this.ascSelect.onclick;
    this.ascSelect.onclick = function(evt) {
        if (!evt) {
            evt = window.event;
        }

        this.autoSelect.setComboValue();
        //this.autoSelect.ascText.focus();
        //this.autoSelect.hideComboList();

        for (i=1; i<this.autoSelect.scripts.length; (i=i+1)) {
            eval(this.autoSelect.scripts[i]);
        }

    }

    this.ascSelect.oldOnFocus = this.ascSelect.onfocus;
    this.ascSelect.onfocus = function(evt) {
        if (!evt) 
            evt = window.event;

        this.autoSelect.selFocused = true;
    }

    this.ascSelect.oldOnBlur = this.ascText.onblur;
    this.ascSelect.onblur = function(evt) {
        if (!evt) 
            evt = window.event;

        this.autoSelect.selFocused = false;
        this.autoSelect.hideComboList();

    }

    this.duplicateElements();

}

我不知道如何进一步调查以及出了什么问题。

我发现了问题,

问题出在函数 showComboList 上,在找到匹配项后,会显示组合列表并获得焦点,结果焦点从输入元素中丢失。

函数内部 AutoSelectCombo.prototype.showComboList = function(){, remove/comment 表示 fs.focus().

的行

&你很高兴。