Jquery 在文本框中最后一个字符后设置焦点

Jquery 在文本框中最后一个字符后设置焦点,jquery,focus,Jquery,Focus,我有3个电话号码文本框。当用户键入时,它会自动从一个文本框移动到下一个文本框。当用户按backspace时,我可以将焦点移到上一个文本框。问题是在IE中,焦点设置在文本框的开头。这是我的代码,它在chrome中运行良好 $('#AreaCode').live('keyup', function (event) { if ($(this).val().length == $(this).attr("maxlength")) $('#Prefix').focus(); });

我有3个电话号码文本框。当用户键入时,它会自动从一个文本框移动到下一个文本框。当用户按backspace时,我可以将焦点移到上一个文本框。问题是在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();
});

向后看时,如何在内容末尾设置焦点?

这是一种简单的方法。如果你要倒退,只需添加
$(“#前缀”).val($(“#前缀”).val()
在你设定焦点之后

这是更合适(更干净)的方式:

任何浏览器的代码:

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();
    }
}

我尝试了很多不同的解决方案,唯一对我有效的是基于Chris G在本页上的解决方案(但稍作修改)

我已经把它变成了一个jQuery插件,供将来需要它的人使用

(function($){
    $.fn.setCursorToTextEnd = function() {
        var $initialVal = this.val();
        this.val($initialVal);
    };
})(jQuery);
用法示例:

$('#myTextbox').setCursorToTextEnd();

$(函数(){
$('#区号,#firstNum,#secNum').keyup(函数(e){
if($(this.val().length==$(this.attr('maxlength'))
$(this).next(':input').focus()
})
})
- 
- 

这对我来说很好。[Ref:Gavin G的非常好的插件]

(function($){
    $.fn.focusTextToEnd = function(){
        this.focus();
        var $thisVal = this.val();
        this.val('').val($thisVal);
        return this;
    }
}(jQuery));

$('#mytext').focusTextToEnd();

您可以按照以下步骤在文本框的最后位置设置指针

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 />

Chris Coyier为此提供了一个迷你jQuery插件,该插件运行良好:

如果支持,它将使用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;
  });
};
然后你可以做:

input.putCursorAtEnd();

这里复制了另一个简单的方法:插入符号而不是克拉。我还将它包装在一个try..catch中以防万一,并在开始处添加“if(elemLen==0){return;}”。此外,代码的第一行的格式与其他行不同。除此之外,谢谢你的代码:)谢谢你的拼写/格式修正。我更新了答案以修复它。很高兴它对你有用。我添加了
elem=$(elem.get(0)位于'var elemLen=elem.value.length;`之前的第2行。如果对象是jQuery对象,它将被转换为javascript对象。如果它仍然是一个javascript对象,这一行不会做任何事情,因为一切都很好:)这一行避免了一个错误“elem.value未定义”。为我工作-值得一提的是,您应该将焦点链接到val的末尾,以提高复制此代码的效率。?+1使其成为插件。在调用此函数之前,请记住对元素调用
focus()。在IE中,它可以追溯到文本的开头。还有其他人经历过这种情况吗?为什么在JavaScript变量前面有一个美元符号?另外,用“var”声明它。@MathiasLykkegaardLorenzen将美元符号放在jQuery变量前面是一些人推荐的一种做法,用于区分jQuery变量和普通JS变量。但在本例中,它完全没有必要,因为该变量仅在函数的作用域内可用,这显然是一个jQuery func。@gavin-g Awesome plugin。一个更新,使它更跨浏览器<代码>此.val(“”).val(初始值)首先清除值似乎有帮助。在尝试了大量答案后,它终于起作用了…:)我正在使用IE8。@加文G版本对我不起作用,但这个版本对我起作用。
var val =$("#inputname").val();
$("#inputname").removeAttr('value').attr('value', val).focus();
// I think this is beter for all browsers...
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;
  });
};
input.putCursorAtEnd();