【发布时间】:2015-06-10 02:38:10
【问题描述】:
(1)表单包含textarea,输入带有换行符的文本。Like :
一个
b
然后提交表单并将文本存储到数据库。内容长度为4。我可以输出它们的ascii代码,使用ord()函数。它们是97(a),13(cr)、10(lf)、98(b)。
(2)使用smarty.Like获取内容并赋值给模板:
//$string is get from database.
$smarty->assign('str', $string);
使用输入元素将内容存储在 html 模板中。
<input id="test" type="hidden" value="{$str}">
(3)获取输入值的长度
document.getElementById("test").value.length; //the result is 3
使用中
{$str|@strlen} //the result is 4
如果我提交表单并获取内容的ASCII码,它们是97(a)、10(lf)、98(b) 。 13(cr) 字符丢失了。
我搜索了很多,但没有找到原因。有什么解释吗?提前非常感谢。
我发现了这个:
https://bugzilla.mozilla.org/show_bug.cgi?id=188015
评论28中有人回复,官方文档转至:
http://www.w3.org/TR/html5/syntax.html#preprocessing-the-input-stream
"CR" (U+000D) characters and "LF" (U+000A) characters are treated specially. All CR characters must be converted to LF characters, and any LF characters that immediately follow a CR character must be ignored.
希望能提供帮助。
【问题讨论】:
-
为什么要将多行文本存储到单行编辑控件中?
-
@Cristik 我调查了之前写的代码。 用作媒介,其他一些代码需要使用这个 来获取值。从这里开始,长度是错误的。
-
@AnoccLinn 我认为@Cristik 的意思是,如果您的数据有多行,则应该使用
<textarea>。 -
@T0xicCode 实际上使用了
<textarea>。但是实现是使用<input>的值设置<textarea>的值。<input>的值是错误的,所以是<textarea>.
标签: javascript php html dom