通过检测 JavaScript 中的 CTRL+A 键来聚焦文本框
Focus the textbox by detecting CTRL+A key in javascript
请找到下面的代码以获取更多信息。
<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;
}
我认为您必须更改 && 到 ||(或)。
相关文章:
- 无法使用 $('input[name=“rate”]').focus() 聚焦文本框;方法
- 想要清除文本框+聚焦文本框+阻止asp.net在同一按钮上回发
- jQuery聚焦文本框并转到末尾,但也可以设置光标的位置,以便用户可以看到它
- 在聚焦文本输入的同时使用箭头键滚动(Javascript)
- 在聚焦文本输入时触发文件选择
- 如何聚焦文本区域单击主类
- 通过检测 JavaScript 中的 CTRL+A 键来聚焦文本框
- 聚焦文本输入时如何单击行?(反应原生)
- 聚焦文本区域时不希望滚动
- Bootstrap 3 Modal内置texarea.当点击/聚焦文本区域时,背景滚动到顶部.仅适用于iOS 8
- 如何选择,聚焦文本区域
- 如何创建一个工具提示,该工具提示在按下大写锁定键时从聚焦文本框弹出
- 在react-native (Android)中关闭键盘时不聚焦文本输入
- 选择聚焦文本
- 使用css聚焦文本区域
- 关闭jquery UI对话框后,聚焦文本框
- 如何在Javascript中聚焦文本框时显示按钮
- 在google chrome扩展中使用javascript聚焦文本框
- 平滑滚动和聚焦文本区域
- 按钮单击可更改动态生成的聚焦文本框中的文本