键入时文本框失去焦点

时间:2015-11-19 09:44:40

标签: javascript jquery onkeyup

我正在处理网络应用程序并遇到问题,文本框在输入时失去焦点。

具体来说,让我解释一下它是如何发生的:

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

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

    以下是用于下拉列表的代码。

    <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();">
    

    UPDATE-1: 函数focusAutoSelect()的代码

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

    UPDATE-2:

    AutoSelectCombo对象:

    这是它的作用:

    这是AutoSelectCombo对象。虽然它始于一个 线性自动选择,现在通过过滤选择。什么 键入此框将用于过滤选项列表 仅包含已键入文本的项目。

    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();
    
    }
    

    我没有得到进一步调查以及出了什么问题。

1 个答案:

答案 0 :(得分:1)

我发现了问题,

问题出在函数showComboList上,找到匹配后,会显示组合列表并进行聚焦,因此焦点会从输入元素中丢失。

在函数AutoSelectCombo.prototype.showComboList = function(){内, 删除/评论显示fs.focus()的行。

&安培;你很高兴。