设置文本框中最后一个字符后的焦点
我有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();
});
如何在倒退时在内容末尾设置焦点?
这是实现它的简单方法。如果你倒退,只需添加 $("#Prefix").val($("#Prefix").val());
后您将焦点设置
这是更合适的(清洁剂)的方式:
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()
代码的任何浏览器:
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();
}
}
终于尝试加载答案后,它的工作...... :) 我使用的IE 8 – Diganta 2015-06-05 12:52:57
我尝试了很多不同的解决方案,唯一一个为我工作的是基于Chris G在本页面上的解决方案(但稍作修改)。
我已经把它变成一个jQuery插件,以备将来使用的人需要它的使用
(function($){
$.fn.setCursorToTextEnd = function() {
var $initialVal = this.val();
this.val($initialVal);
};
})(jQuery);
例如:
$('#myTextbox').setCursorToTextEnd();
<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>
这工作对我很好。 [参考:非常漂亮的插件由加文·G]
(function($){
$.fn.focusTextToEnd = function(){
this.focus();
var $thisVal = this.val();
this.val('').val($thisVal);
return this;
}
}(jQuery));
$('#mytext').focusTextToEnd();
@Gavin G版本不适用于我,但这是一个。 – 2017-09-27 22:17:27
你可以在文本框的最后一个位置按以下设置指针。
temp=$("#txtName").val();
$("#txtName").val('');
$("#txtName").val(temp);
$("#txtName").focus();
你应该这样编码。
var num = $('#Number').val();
$('#Number').focus().val('').val(num);
可以在输入标签中使用这些简单的javascript。
<input type="text" name="your_name" value="your_value" onfocus="this.setSelectionRange(this.value.length, this.value.length);" autofocus />
var val =$("#inputname").val();
$("#inputname").removeAttr('value').attr('value', val).focus();
// I think this is beter for all browsers...
的重复http://stackoverflow.com/questions/511088/use-javascript-to-place-cursor-at-end-of-text-in-text-input-element – 2011-01-05 21:40:12
另一个简单的方法在这里:http://stackoverflow.com/a/19568146/1032372 – shim 2014-11-25 21:29:37