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

Focus the textbox by detecting CTRL+A key in javascript

本文关键字:聚焦 文本 CTRL+A 检测 JavaScript 中的      更新时间:2023-09-26

请找到下面的代码以获取更多信息。

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

我认为您必须更改 && 到 ||(或)。