jQuery文本框光标到文本结尾? [英] jQuery Textbox Cursor to end of Text?

查看:17
本文介绍了jQuery文本框光标到文本结尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use jQuery to basically replace the cursor at the end of the text in a textbox AFTER the user hits "enter"

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

I have the "enter" part working - but I've no idea how [after the enter part] - I can get the cursor to return to the end of the inputted text inside the textbox ?

即目前,当用户按 Enter 键时 - 光标转到新行,我希望它基本上转到当前文本的末尾?

i.e. at the moment, when the user hits enter - the cursor goes to a new line and I want it to basically go to the end of the current text?

一些代码:

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来阻止它.p>

If you just want to prevent the 'enter' key from creating a newline, you can use preventDefault to block it.

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

小提琴

如果您真的想在输入中按 anywhere 进入输入的末尾,您还可以重置将始终将光标放在输入末尾的值:

If you really want enter pressed anywhere in the input to go to the end of the input, you can also reset the value which will always put the cursor at the end of the input:

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

这篇关于jQuery文本框光标到文本结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆