jQuery 在文本框中的最后一个字符后设置焦点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4609405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set Focus After Last Character in Text Box
提问by Josh
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.
我有一个电话号码的 3 个文本框。当用户键入时,它会自动从一个文本框移动到下一个文本框。当用户按下退格键时,我可以将焦点移到上一个文本框。问题是在 IE 中,焦点设置在文本框的开头。这是我的代码,它在 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?
后退时如何将焦点设置在内容的末尾?
回答by ChrisG
This is the easy way to do it. If you're going backwards, just add
$("#Prefix").val($("#Prefix").val());
after you set the focus
这是最简单的方法。如果要倒退,只需$("#Prefix").val($("#Prefix").val());
在设置焦点后添加
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()
回答by Gavin G
I tried lots of different solutions, the only one that worked for me was based on the solution by Chris G on this page (but with a slight modification).
我尝试了很多不同的解决方案,唯一对我有用的解决方案是基于 Chris G 在此页面上的解决方案(但稍作修改)。
I have turned it into a jQuery plugin for future use for anyone that needs it
我已经把它变成了一个 jQuery 插件,供以后需要它的人使用
(function($){
$.fn.setCursorToTextEnd = function() {
var $initialVal = this.val();
this.val($initialVal);
};
})(jQuery);
example of usage:
用法示例:
$('#myTextbox').setCursorToTextEnd();
回答by Devasish
This works fine for me . [Ref: the very nice plug in by Gavin G]
这对我来说很好用。[参考:Gavin G 非常好的插件]
(function($){
$.fn.focusTextToEnd = function(){
this.focus();
var $thisVal = this.val();
this.val('').val($thisVal);
return this;
}
}(jQuery));
$('#mytext').focusTextToEnd();
回答by Arjun
You should code like this.
你应该像这样编码。
var num = $('#Number').val();
$('#Number').focus().val('').val(num);
回答by Guilherme
Code for any Browser:
任何浏览器的代码:
function focusCampo(id){
var inputField = document.getElementById(id);
if (inputField != null && inputField.value.length != 0){
if (inputField.createTextRange){
var FieldRange = inputField.createTextRange();
FieldRange.moveStart('character',inputField.value.length);
FieldRange.collapse();
FieldRange.select();
}else if (inputField.selectionStart || inputField.selectionStart == '0') {
var elemLen = inputField.value.length;
inputField.selectionStart = elemLen;
inputField.selectionEnd = elemLen;
inputField.focus();
}
}else{
inputField.focus();
}
}
回答by Dens
you can set pointer on last position of textbox as per following.
您可以按照以下方式在文本框的最后一个位置设置指针。
temp=$("#txtName").val();
$("#txtName").val('');
$("#txtName").val(temp);
$("#txtName").focus();
回答by Santosh Satapathy
One can use these simple javascript within the input tag.
可以在输入标签中使用这些简单的 javascript。
<input type="text" name="your_name" value="your_value"
onfocus="this.setSelectionRange(this.value.length, this.value.length);"
autofocus />
回答by mangas
var val =$("#inputname").val();
$("#inputname").removeAttr('value').attr('value', val).focus();
// I think this is beter for all browsers...
回答by Prateek
Chris Coyier has a mini jQuery plugin for this which works perfectly well: http://css-tricks.com/snippets/jquery/move-cursor-to-end-of-textarea-or-input/
Chris Coyier 有一个迷你 jQuery 插件,效果很好:http: //css-tricks.com/snippets/jquery/move-cursor-to-end-of-textarea-or-input/
It uses setSelectionRange if supported, else has a solid fallback.
如果支持,它使用 setSelectionRange,否则有一个可靠的后备。
jQuery.fn.putCursorAtEnd = function() {
return this.each(function() {
$(this).focus()
// If this function exists...
if (this.setSelectionRange) {
// ... then use it (Doesn't work in IE)
// Double the length because Opera is inconsistent about whether a carriage return is one character or two. Sigh.
var len = $(this).val().length * 2;
this.setSelectionRange(len, len);
} else {
// ... otherwise replace the contents with itself
// (Doesn't work in Google Chrome)
$(this).val($(this).val());
}
// Scroll to the bottom, in case we're in a tall textarea
// (Necessary for Firefox and Google Chrome)
this.scrollTop = 999999;
});
};
Then you can just do:
然后你可以这样做:
input.putCursorAtEnd();
回答by Rajkumar
<script type="text/javascript">
$(function(){
$('#areaCode,#firstNum,#secNum').keyup(function(e){
if($(this).val().length==$(this).attr('maxlength'))
$(this).next(':input').focus()
})
})
</script>
<body>
<input type="text" id="areaCode" name="areaCode" maxlength="3" value="" size="3" />-
<input type="text" id="firstNum" name="firstNum" maxlength="3" value="" size="3" />-
<input type="text" id="secNum" name=" secNum " maxlength="4" value="" size="4" />
</body>