[英]HTML textarea linebreak on enter key press don't work
I have a textarea and when click enter it doesn't insert a linebreak, I tried to use the following code, But, when i press enter, it goes to the end fo text and add new line. 我有一个textarea,单击Enter时未插入换行符,我尝试使用以下代码,但是,按Enter时,它将转到文本末尾并添加新行。
$("#descre").on('keydown', function(e) {
var code = e.keyCode || e.which;
if (code == 13) { //Enter keycode
event.preventDefault();
var s = $(this).val();
$(this).val(s + "\n");
}
});
I want to make a normal enter press, like ex: jsfiddle 我想做一个普通的回车,例如: jsfiddle
My textarea was not working well, so to accept enter key press on normal behaviour i used the code: 我的textarea运行不正常,因此我接受使用正常代码输入回车的代码:
$('#descre').keypress(function(e) {
if (e.keyCode == 13) {
e.preventDefault();
this.value = this.value.substring(0, this.selectionStart) + "" + "\n" + this.value.substring(this.selectionEnd, this.value.length);
}
});
You have passed e in the function as argument but in the if block you are doing event.preventDefault()
change it to e.preventDefault()
您已经在函数中将e作为参数传递了,但是在if块中,您正在执行
event.preventDefault()
将其更改为e.preventDefault()
//i try this one
$("#descre").on('keydown', function(e) {
var code = e.keyCode || e.which;
if(code == 13) { //Enter keycode
e.preventDefault();
var s = $(this).val();
$(this).val(s);
}
});
You are adding an extra line in the end yourself. 您最终会自己添加一条额外的行。 Thats why it is inserting the extra k=line
那就是为什么它要插入额外的k = line
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.