Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过检测CTRL+;javascript中的键_Javascript_Html_Autocomplete_Textbox - Fatal编程技术网

通过检测CTRL+;javascript中的键

通过检测CTRL+;javascript中的键,javascript,html,autocomplete,textbox,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&g

请查找以下代码以了解更多信息

<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>
javascript文件:

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'}
]
};
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); 

}
}
功能自动建议控件(文本框,建议){
this.textbox=textbox;
这.建议=建议;
var-caretpos=0;
init();
功能请求建议(AutoSuggestControl){
控制台日志(“请求建议”);
var This=此;
var建议值=[];
caretpos=textbox.selectionStart;
if(textbox.selectionStart | | textbox.selectionStart=='0')
var testextboxval=textbox.value.substr(0,caretpos);
如果(ExtBoxVal.length>0)
{
for(var i=0;i0){
提前输入(建议值[0]);
console.log(suggestedvalue[0]);
}
否则{
textbox.value=testextboxval;
}
}
函数类型前置(建议值){
if(textbox.createTextRange | | textbox.setSelectionRange)
{
var txtLen=caretpos;
textbox.value=建议值;
setCaretPosition(txtLen,suggestedvalue.length);
}
}
函数setCaretPosition(txtLen、sugLen){
if(textbox.createTextRange)
{
var oRange=textbox.createTextRange();
橙色。移动开始(“字符”,sugLen);
oRange.moveEnd(“字符”,txtLen-textbox.value.length);
橙色。选择();
//对Mozilla使用setSelectionRange()
} 
else if(textbox.setSelectionRange)
{
textbox.setSelectionRange(sugLen、txtLen);
}
//this.lastCaret=txtLen;
//将焦点设置回文本框
textbox.focus();
}
自动建议();
}
函数init(){
var oThis=这个;
//分配onkeyup事件处理程序
textbox.onkeyup=函数(事件){
//检查事件对象的正确位置
如果(!事件){
event=window.event;
}    
console.log(文本框);
//使用事件对象调用handleKeyUp()方法
handleKeyUp(事件);
};
}
函数handleKeyUp(事件){
var e=事件;
var=0;
var iKeyCode=event.keyCode;
document.onkeydown=函数(e){
如果(iKeyCode==65&&e.ctrlKey)
{stay=1;
}
控制台日志(“停留”+停留);
如果(保持==1)
textbox.focus();
其他的
请求建议(自动建议控制);
}
}
我正在创建一个自动提示文本框,但当我按
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;
}

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

当您单击Ctrl+A时

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

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

我已经尝试过了,但是如果您通常键入“a”,那么该条件也会得到满足,并且会导致不希望的结果。例如,当您要键入“pay”时。我还想更改尚未键入的建议字符串的颜色。有什么想法吗?如何做到这一点。除了focus()。我已经尝试过了,但是如果您正常键入“a”,那么该条件也会得到满足,并且会导致不想要的结果。例如,当您要键入“pay”时。我还想更改尚未键入的建议字符串的颜色。对此有何想法?如何操作。除了focus()。