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

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

问题描述

我试图用jQuery来基本上替换文本末尾的文本框中的光标后用户点击enter



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



即目前,当用户点击进入 - 光标到一个新的行,我希望它基本上到当前文本的结尾?

一些代码:


$ b $ pre $ jQuery('#textbox')。keyup(function(e){
if(e.keyCode == 13){
... submits textbox
}
jQuery(this).focus(function(){
var val = this.input.value; //存储值元素
this.input.value =''; //清除元素的值
this.input.value = val; //设置该值
)};
});


解决方案

如果您只是想阻止回车从创建换行符开始,可以使用 preventDefault 来阻止它。

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

小提琴



如果您确实想要在输入中的任意位置按 以进入输入的末尾,您也可以重置将始终把光标放在输入结尾的值:

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


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 ?

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?

Some code:

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

解决方案

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

fiddle

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天全站免登陆