在 textarea 中按回车键时如何添加一行而不是 space?
How to add a line instead of a space when pressing enter in textarea?
我有一个 textarea
表单,当用户按下 enter
时会显示新行,但是作为 $_POST
变量,它将 enters
保存为 space秒。我怎样才能改变它,以便当用户按下回车键时,它会在文本区域中插入一个不间断的 space,而不是单个 space?
示例:
document.onPresskeySpace(insertBrElementInTextarea);
对于不知道文本区域长什么样的人,这是我的表格:
<form method="POST" action="process-page.php">
<input autocomplete="off" style="height: 60px; width: 95%; margin-left: 2.5%; box-sizing: border-box" class="form-control" type="text" name="name" placeholder="name"><br>
<textarea autocomplete="off" required="" style="min-height: 50px; max-height: 275px; width: 95%; margin-left: 2.5%; box-sizing: border-box" class="form-control" name="msg"></textarea><br>
<button style="width: 95%; margin-left: 2.5%; box-sizing: border-box" type="submit" name="submit" class="btn btn-dark">Publish</button>
</form>
实际上,他们没有将“enter”保存为“space”,而是将其保存为“\n”(新行)
-- https://en.wikipedia.org/wiki/Newline#In_programming_languages
有些人很困惑,因为这个字符没有显示在数据库软件或 phpmyadmin 中。
我有一个建议。
如果您想在您的网站上使用 php 显示它,您可以随时将此字符 (\n) 更改为 <br/>
...
<php echo str_replace("\n", "<br/>", $your_variable); ?>
...
为了澄清目的
不间断 space 是 不是 您应该使用不间断 space 的术语(html 实体:
) 不插入换行符。它做相反的事情:即它防止两个词之间的换行...
例如,假设您想确保一个人的名字不会在中途换行,尽管您会使用类似的东西:
firstname lastname
在这种情况下,全名总是在一行;如果它的任何部分需要 broken 到新行然后 whole 将被放在新行上。
例子
// Given the string
Hello there, my name is James Mason
// If the line limit is 30 characters (i.e. part way through the name then it'll output as
Hello there, my name is
James Mason
// If the string were not to have a non-breaking space
Hello there, my name is James Mason
// The output would be
Hello there, my name is James
Mason
可在此处找到更多信息:
如何修复
假设您要将文本输入表单中的标准文本区域:
<form method="post" action="">
<textarea name="inputText"></textarea>
<input type="submit" value="Send Text">
</form>
然后当你按回车键时(如@HaniefHan所述)输入的字符是\n
,你通常看不到和在HTML不是输出。例如:
echo "this is a
sentence that spans
over multiple lines";
将在一行上输出为:
this is a sentence that spans over multiple lines
但是,如果您满足以下条件,您仍然可以看到原始文本(在单独的行上):
- 查看页面源码
- 输出
<pre>
个标签内的文本
但是,由于我们倾向于在 html 标签(例如 <div>...</div>
)内输出文本数据,因此它们将全部显示在一行中;假设屏幕宽度大于文本长度。
如果您想在 HTML 中显示换行符,则需要将 \n
替换为 <br>
,或者根据您的使用情况,将段落换行 <p>
(或替代标签)。
// Using the example form from earlier
echo str_replace("\n", "<br>", $_POST["inputText"]);
完整代码示例
echo "
<form method='post' action=''>
<textarea name='inputText'></textarea>
<input type='submit' value='Send Text'>
</form>
";
echo str_replace("\n", "<br>", $_POST["inputText"]);
// OR
# echo nl2br($_POST["inputText"]);
输入的文字:
This is text with
multiple lines
of
input text.
原始输出:
This is text with<br>multiple lines<br>of<br>input text.
显示为:
This is text with
multiple lines
of
input text.
也许您想使用 nl2br() ?
我有一个 textarea
表单,当用户按下 enter
时会显示新行,但是作为 $_POST
变量,它将 enters
保存为 space秒。我怎样才能改变它,以便当用户按下回车键时,它会在文本区域中插入一个不间断的 space,而不是单个 space?
示例:
document.onPresskeySpace(insertBrElementInTextarea);
对于不知道文本区域长什么样的人,这是我的表格:
<form method="POST" action="process-page.php">
<input autocomplete="off" style="height: 60px; width: 95%; margin-left: 2.5%; box-sizing: border-box" class="form-control" type="text" name="name" placeholder="name"><br>
<textarea autocomplete="off" required="" style="min-height: 50px; max-height: 275px; width: 95%; margin-left: 2.5%; box-sizing: border-box" class="form-control" name="msg"></textarea><br>
<button style="width: 95%; margin-left: 2.5%; box-sizing: border-box" type="submit" name="submit" class="btn btn-dark">Publish</button>
</form>
实际上,他们没有将“enter”保存为“space”,而是将其保存为“\n”(新行)
-- https://en.wikipedia.org/wiki/Newline#In_programming_languages
有些人很困惑,因为这个字符没有显示在数据库软件或 phpmyadmin 中。
我有一个建议。
如果您想在您的网站上使用 php 显示它,您可以随时将此字符 (\n) 更改为 <br/>
...
<php echo str_replace("\n", "<br/>", $your_variable); ?>
...
为了澄清目的
不间断 space 是 不是 您应该使用不间断 space 的术语(html 实体:
) 不插入换行符。它做相反的事情:即它防止两个词之间的换行...
例如,假设您想确保一个人的名字不会在中途换行,尽管您会使用类似的东西:
firstname lastname
在这种情况下,全名总是在一行;如果它的任何部分需要 broken 到新行然后 whole 将被放在新行上。
例子
// Given the string
Hello there, my name is James Mason
// If the line limit is 30 characters (i.e. part way through the name then it'll output as
Hello there, my name is
James Mason
// If the string were not to have a non-breaking space
Hello there, my name is James Mason
// The output would be
Hello there, my name is James
Mason
可在此处找到更多信息:
如何修复
假设您要将文本输入表单中的标准文本区域:
<form method="post" action="">
<textarea name="inputText"></textarea>
<input type="submit" value="Send Text">
</form>
然后当你按回车键时(如@HaniefHan所述)输入的字符是\n
,你通常看不到和在HTML不是输出。例如:
echo "this is a
sentence that spans
over multiple lines";
将在一行上输出为:
this is a sentence that spans over multiple lines
但是,如果您满足以下条件,您仍然可以看到原始文本(在单独的行上):
- 查看页面源码
- 输出
<pre>
个标签内的文本
但是,由于我们倾向于在 html 标签(例如 <div>...</div>
)内输出文本数据,因此它们将全部显示在一行中;假设屏幕宽度大于文本长度。
如果您想在 HTML 中显示换行符,则需要将 \n
替换为 <br>
,或者根据您的使用情况,将段落换行 <p>
(或替代标签)。
// Using the example form from earlier
echo str_replace("\n", "<br>", $_POST["inputText"]);
完整代码示例
echo "
<form method='post' action=''>
<textarea name='inputText'></textarea>
<input type='submit' value='Send Text'>
</form>
";
echo str_replace("\n", "<br>", $_POST["inputText"]);
// OR
# echo nl2br($_POST["inputText"]);
输入的文字:
This is text with
multiple lines
of
input text.
原始输出:
This is text with<br>multiple lines<br>of<br>input text.
显示为:
This is text with
multiple lines
of
input text.
也许您想使用 nl2br() ?