2016-02-26 28 views
1

請查找以下代碼以獲取更多信息。通過在javascript中檢測CTRL + A鍵來聚焦文本框

<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鍵文本框變爲空白。它應該在保留光標位置的同時聚焦文本框。請幫助我。

非常感謝。

回答

0

當你按Ctrl + A,

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

我認爲你必須改變& &到||(或)。

+0

我已經嘗試過,但如果通常輸入「a」,那麼該條件也會得到滿足,並且會導致不需要的結果。比如當你想輸入「付費」時。我也想改變尚未輸入建議字符串的顏色。有關於此的任何想法?怎麼做。除focus()外。 – soumya

相關問題