jQuery文本框光标到文本结尾?

Tom*_*Tom 8 jquery textbox focus

我试图使用jQuery基本上替换用户点击"输入"后文本框中文本末尾的光标

我有"输入"部分工作 - 但我不知道[在输入部分之后] - 我可以让光标返回到文本框内输入文本的末尾?

即在此刻,当用户点击进入时 - 光标转到一个新行,我希望它基本上到达当前文本的末尾?

一些代码:

jQuery('#textbox').keyup(function (e) {
        if (e.keyCode == 13) {
            ... submits textbox
        }
        jQuery(this).focus(function() {
          var val = this.input.value; //store the value of the element
          this.input.value = ''; //clear the value of the element
          this.input.value = val; //set that value back.  
        )};    
});
Run Code Online (Sandbox Code Playgroud)

Dav*_*ton 6

如果您只是想阻止'enter'键创建换行符,可以使用preventDefault它来阻止它.

$("textarea").keypress(function (event) {
    if (event.which == '13') {
        event.preventDefault();
    }
});
Run Code Online (Sandbox Code Playgroud)

小提琴

如果你真的想在输入的任何地方按下输入来转到输入的末尾,你也可以重置值,它总是把光标放在输入的末尾:

$("textarea").keypress(function (event) {
    if (event.which == '13') {
        var val = $(this).val();       
        $(this).val('');
        $(this).val(val);
        event.preventDefault();
    }
});
Run Code Online (Sandbox Code Playgroud)