gpt4 book ai didi

javascript - 通过检测 javascript 中的 CTRL+A 键来聚焦文本框

转载 作者:行者123 更新时间:2023-11-28 06:19:38 25 4
gpt4 key购买 nike

请查找以下代码以获取更多信息。

<html>
<head>
<title>Autosearch</title>
<script type="text/javascript" src="autosuggest.js"></script>
<script type="text/javascript" src="suggestion.js"></script>

<style>
#txtbox{color:Black; width:200px};
</style>
</head>

<body>
<p>search</p>
<p><input type="text" id="txtbox" /></p>

<script type="text/javascript">
window.onload = function () {
var textbox = new AutoSuggestControl
(document.getElementById("txtbox"),suggestions);
}
</script>
</body>
</html>

json 对象:

suggestions= {
keyword :
[
{child1:'Pay who? How Much? When?', child2:' who?'},
{child1:'Balance', child2:'type?'},
{child1:'Beneficiary', child2:'add'},
{child1:'Change Passcode'},
{child1:'Change Memorable Word'}
]
};

javascript 文件:

function AutoSuggestControl(textbox,suggestions) {
this.textbox=textbox;
this.suggestions=suggestions;
var caretpos=0;
init();


function requestsuggestion(AutoSuggestControl){
console.log("requestsuggestion");
var This=this;
var suggestedvalue = [];
caretpos=textbox.selectionStart;
if(textbox.selectionStart || textbox.selectionStart == '0')
var temptextboxval=textbox.value.substr(0,caretpos);
if(temptextboxval.length > 0)
{

for (var i=0; i < This.suggestions.keyword.length; i++) {
if
(suggestions.
keyword[i].
child1.toLowerCase().indexOf
(temptextboxval.toLowerCase () ) == 0)
{
suggestedvalue.push(This.suggestions.keyword[i].child1);
console.log(suggestions.keyword[i].child1);
}
}
}

function autosuggest() {
if(suggestedvalue.length > 0){
typeAhead(suggestedvalue[0]);
console.log(suggestedvalue[0]);
}
else{
textbox.value=temptextboxval;
}

}

function typeAhead(suggestedvalue) {

if (textbox.createTextRange || textbox.setSelectionRange)
{
var txtLen=caretpos;
textbox.value=suggestedvalue;
setCaretPosition(txtLen,suggestedvalue.length);
}

}

function setCaretPosition(txtLen,sugLen){

if (textbox.createTextRange)
{
var oRange = textbox.createTextRange();
oRange.moveStart("character", sugLen);
oRange.moveEnd("character", txtLen - textbox.value.length);

oRange.select();


//use setSelectionRange() for Mozilla
}
else if (textbox.setSelectionRange)
{
textbox.setSelectionRange(sugLen,txtLen);


}
//this.lastCaret=txtLen;

//set focus back to the textbox
textbox.focus();
}

autosuggest();
}


function init() {
var oThis = this;
//assign the onkeyup event handler
textbox.onkeyup = function (event) {
//check for the proper location of the event object
if (!event) {
event = window.event;


}
console.log(textbox);
//call the handleKeyUp() method with the event object
handleKeyUp(event);


};
}

function handleKeyUp(event) {
var e=event;
var stay=0;
var iKeyCode = event.keyCode;

document.onkeydown=function(e){
if(iKeyCode==65 && e.ctrlKey)
{stay=1;
}
console.log("stay"+stay);
if(stay==1)
textbox.focus();
else
requestsuggestion(AutoSuggestControl);

}
}

我正在创建一个自动建议文本框,但是当我按 ctrl+A 键时,文本框变为空白。它应该聚焦文本框,同时保留光标位置。请帮帮我。

提前谢谢您。

最佳答案

当您单击 Ctrl+A 时,

if(iKeyCode==65 && e.ctrlKey)
{
//this block does not work , because you will click first Ctrl, then A(65)
stay=1;
}

我认为你必须将 && 更改为 ||(或)。

关于javascript - 通过检测 javascript 中的 CTRL+A 键来聚焦文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35651073/

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