在文本框中设置最后一个字符后的焦点 [英] Set Focus After Last Character in Text Box
本文介绍了在文本框中设置最后一个字符后的焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ b $ $ p $ $'''''''''''''''''''''''''' ($(this).val()。)
('#Prefix')。 b $ b}); live('keyup',function(event){
if(event.keyCode == 8&& $(this).val($) ).length == 0)
$('#AreaCode')。focus();
$ b $ if($(this).val()。length == $(this)。 attr(maxlength))
$('#Number')。focus();
}); live('keyup',function(event){
if(event.keyCode == 8&& $(this).val($) ).length == 0)
$('#Prefix')。focus();
});
如何在倒退的内容末尾设置焦点?
解决方案
这是简单的方法。如果您要倒退,只需添加
$(#Prefix)。val($(#Prefix).val());
$
这是更合适(更干净)的方式:
函数SetCaretAtEnd(elem){
var elemLen = elem.value.length;
//对于IE只有
if(document.selection){
//设置焦点
elem.focus();
//使用IE范围
var oSel = document.selection.createRange();
//将位置重置为0&然后设置在
结尾oSel.moveStart('character',-elemLen);
oSel.moveStart('character',elemLen);
oSel.moveEnd('character',0);
oSel.select();
else if(elem.selectionStart || elem.selectionStart =='0'){
// Firefox / Chrome
elem.selectionStart = elemLen;
elem.selectionEnd = elemLen;
elem.focus();
} // if
} // SetCaretAtEnd()
I have 3 text boxes for a phone number. As the user types, it automatically moves from one textbox to the next. When the user presses backspace, I can move focus to the previous text box. The problem is that in IE, focus is set to the beginning of the text box. Here's my code, which works fine in chrome.
$('#AreaCode').live('keyup', function (event) {
if ($(this).val().length == $(this).attr("maxlength"))
$('#Prefix').focus();
});
$('#Prefix').live('keyup', function (event) {
if (event.keyCode == 8 && $(this).val().length == 0)
$('#AreaCode').focus();
if ($(this).val().length == $(this).attr("maxlength"))
$('#Number').focus();
});
$('#Number').live('keyup', function (event) {
if (event.keyCode == 8 && $(this).val().length == 0)
$('#Prefix').focus();
});
How do I make the focus set at the end of the contents when going backwards?
解决方案
This is the easy way to do it. If you're going backwards, just add
$("#Prefix").val($("#Prefix").val());
after you set the focus
This is the more proper (cleaner) way:
function SetCaretAtEnd(elem) {
var elemLen = elem.value.length;
// For IE Only
if (document.selection) {
// Set focus
elem.focus();
// Use IE Ranges
var oSel = document.selection.createRange();
// Reset position to 0 & then set at end
oSel.moveStart('character', -elemLen);
oSel.moveStart('character', elemLen);
oSel.moveEnd('character', 0);
oSel.select();
}
else if (elem.selectionStart || elem.selectionStart == '0') {
// Firefox/Chrome
elem.selectionStart = elemLen;
elem.selectionEnd = elemLen;
elem.focus();
} // if
} // SetCaretAtEnd()
这篇关于在文本框中设置最后一个字符后的焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文