我正在尝试使用 jQuery 在用户点击“enter”后基本上替换文本框中文本末尾的光标
我的“输入”部分正在工作 - 但我不知道[输入部分之后]如何 - 我可以让光标返回到文本框中输入的文本的末尾?
即此刻,当用户按 Enter 键时 - 光标转到新行,我希望它基本上转到当前文本的末尾?
一些代码:
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
如果你真的想按 Enter 键anywhere在输入中转到输入末尾,您还可以重置该值,该值将始终将光标置于输入末尾:
$("textarea").keypress(function (event) {
if (event.which == '13') {
var val = $(this).val();
$(this).val('');
$(this).val(val);
event.preventDefault();
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)