[英]Want to capture 'Enter' key press in HTML form field
I have a HTML form text field( this is a form field),where I want to capture the whole text the end user put into that field along with the Enter key pressed for new lines.Like if i give an example, lets assume, I am putting the below string into the form text field exactly as it looks here: 我有一个HTML表单文本字段(这是一个表单字段),我想捕获最终用户放入该字段的整个文本以及按下新行的Enter键。如果我给出一个例子,让我们假设,我将下面的字符串放在表单文本字段中,就像它在这里看到的一样:
I
Pressed
Enter for a new Line.
Now, I want to save this filed input into the database exactly as end user filled it,for that My database string may looks like this 现在,我想将这个字段输入保存到数据库中,就像最终用户填充它一样,因为我的数据库字符串可能如下所示
"I</br>Pressed</br>Enter for a new Line."
That is okey with me.But i don't want to store it like "I Pressed Enter for a new Line." 这对我来说很好。但我不想把它存储起来,就像“我为一条新线路压入”。
I have done it anyway.But there are many issues in my coding. 无论如何我已经完成了。但是我的编码有很多问题。 Also, my requirement is it should be implemented in such a way, show that the same logic could be applied when the form will be open for editing and there will already be value in that text field from Database. 此外,我的要求是它应该以这种方式实现,表明当表单打开进行编辑时可以应用相同的逻辑,并且数据库中的该文本字段中已经存在值。
Can anybody please help me? 有人可以帮帮我吗?
you can use the replace()
method for that, 你可以使用replace()
方法,
$("textarea").val().replace(/\n/g, "<br />")
Edit 编辑
function captureEnter() {
if (window.event.keyCode == 13) {
document.getElementById("txtArea").value =document.getElementById("txtArea").value + "<br/>";
return false;
}
else {
return true;
}
}
HTML HTML
<textarea id="txtArea" onkeypress="captureEnter();"></textarea>
If I understand you correctly, you want to capture when someone presses enter in a an input
of type=text
, and replace it with <br/>
. 如果我理解正确,你想要在有人按下输入type=text
的input
时捕获,并用<br/>
替换它。 If so, use the onkepress event, and listen for charcode 13, then add the " 如果是这样,使用onkepress事件,并侦听charcode 13,然后添加“
" manually. “手动。
var tf = document.getElementById('myTextFIeld');
//or whatever method to find it you prefer.
tf.onkeypress = function(e) {
//or addEventListener('keypress',...) if you'd rather
if (e.charCode === 13) {
this.value += '<br/>'
}
}
You could also do it server side with PHP and nl2br() : 你也可以用PHP和nl2br()来做服务器端:
$thetext = nl2br($_GET['textarea']);
http://php.net/manual/en/function.nl2br.php http://php.net/manual/en/function.nl2br.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.