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

时间:2016-02-26 11:56:14

标签: javascript html autocomplete textbox

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

<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键时,文本框变为空白。它应该在保留光标位置的同时聚焦文本框。请帮帮我。

谢谢你。

1 个答案:

答案 0 :(得分:0)

单击Ctrl + A时,

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

我认为你必须改变&amp;&amp;到||(或)。

相关问题