微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我试图使用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.  
        )};    
});

解决方法

如果您只是想阻止’enter’键创建换行符,可以使用preventDefault来阻止它.
$("textarea").keypress(function (event) {
    if (event.which == '13') {
        event.preventDefault();
    }
});

fiddle

如果你真的想在输入中的任何地方按下输入以转到输入的末尾,你也可以重置将始终将光标放在输入末尾的值:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐