gpt4 book ai didi

javascript - 文本框在打字时失去焦点

转载 作者:行者123 更新时间:2023-11-27 23:39:42 25 4
gpt4 key购买 nike

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

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

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

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

更新1:focusAutoSelect() 函数的代码

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

更新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();

}

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

最佳答案

我发现了问题,

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

函数内部AutoSelectCombo.prototype.showComboList = function(){,删除/注释 fs.focus() 行。

您可以开始了。

关于javascript - 文本框在打字时失去焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33800207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com