【问题标题】:Setting focus to the end of textbox using javascript使用javascript将焦点设置到文本框的末尾
【发布时间】:2013-03-19 15:44:11
【问题描述】:

我已经编写了下面的代码来将焦点设置到文本框中的文本末尾:

var textbox = document.getElementById('txtFilter');
textbox.focus(); 
textbox.value = textbox.value; 

但是我在这里遇到了一个问题。当我继续快速打字时,新输入的字符没有出现,它一次又一次地显示旧值。所以我必须在按下每个键后等待,然后它才能正常工作。

我找到了另一段代码来实现这一点:

if (textbox.createTextRange) {               
var FieldRange = textbox.createTextRange();            
FieldRange.moveStart('character', textbox.value.length);
FieldRange.collapse();
FieldRange.select();
return false;
}    

但是当我尝试这个时,我得到了错误 - 由于错误 800a025e 有时无法完成操作。请帮我。任何帮助将不胜感激。

【问题讨论】:

标签: c# javascript jquery textbox focus


【解决方案1】:

你可以使用

$('#txtFilter').focus(); //focus using jQuery (browser indipendent)
$('#txtFilter').caretToEnd(); // move position to the end

不幸的是你需要一个插件https://gist.github.com/DrPheltRight/1007907

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多