【问题标题】:Symbol "+" disappear inside input text when post form发布表单时,符号“+”在输入文本中消失
【发布时间】:2012-06-17 06:30:23
【问题描述】:
我正在使用 Zend 框架。我有一个包含一些字段的表单,它们都是type="text",包括一个名为“电话”的输入。提交时,我使用 Ajax 将数据发送到控制器。那么这是我的问题,如果我输入 + 符号,例如:
+34-666666666 我收到的数据是34-666666666。 + 符号变成一个空格。这个问题只发生在+,我尝试了所有的符号,没有问题。我快疯了,我在 Google 中没有找到任何解决方案。
【问题讨论】:
标签:
php
ajax
forms
zend-framework
textinput
【解决方案1】:
您需要按照here 的说明对 GET/POST 数据进行编码。 JavaScript encodeURIComponent 函数可以稍加改动*:
对于 application/x-www-form-urlencoded (POST),根据规范,空格是
被 '+' 取代,因此可能希望遵循一个 encodeURIComponent
将“%20”替换为“+”。
"&phone=" + encodeURIComponent("+34-666666666").replace(/%20/, "+"); // "&phone=%2B34-666666666"
"&phone=" + encodeURIComponent("+34-666666666 x123").replace(/%20/, "+"); // "&phone=%2B34-666666666+x123"
*注意:URL 中+ 字符的处理方式取决于它是用于路径组件还是查询字符串中:
-
http://example.com/page+1 -- 文件名page+1
-
http://example.com/page%201 -- 文件名page 1
-
http://example.com/?file=page+1 -- 查询字符串参数file=page 1
-
http://example.com/?file=page%201 -- 查询字符串参数file=page 1