php 如何使用jQuery在表单输入中转义“&”符号字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5020345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:33:32  来源:igfitidea点击:

How to escape "&" ampersand character in form input using jQuery

phpjqueryajaxcodeigniter

提问by jameserie

I have a problem with my jQuery script that send data via POST method. The problem is whenever it sends data via Ajax that has an "&" ampersand in the sentence, it will cut the sentence when found "&".

我的 jQuery 脚本通过 POST 方法发送数据时遇到问题。问题是每当它通过 Ajax 发送数据时,在句子中有一个“&”符号时,它会在找到“&”时切断句子。

Please check the images below for more info.

请查看下面的图片了解更多信息。

The text that pass to ajax POSTOutput after ajax POST on firebugData that has been save in the databaseThe jQuery ajax POST

传递给 ajax POST 的文本在萤火虫上 ajax POST 后的输出已保存在数据库中的数据jQuery ajax POST

回答by RobertPitt

htmlentites

htmlentites

This function is identical to htmlspecialchars() in all ways, except with htmlentites(), all characters which have HTML character entity equivalents are translated into these entities.

If you're wanting to decode instead (the reverse) you can use html_entity_decode().

此函数在所有方面都与htmlspecialchars()相同,除了htmlentites() 之外,所有具有 HTML 字符实体等效项的字符都将转换为这些实体。

如果你想解码(相反),你可以使用html_entity_decode()。

Example:

例子:

echo htmlentities("&"); // &

if your directly doing this in the browser you should be able to use:

如果您直接在浏览器中执行此操作,您应该可以使用:

encodeURIComponent(string input);

Example:

例子:

encodeURIComponent($.trim($("input[name=t-tim_rendered-"+id+"]").val().toString()));

回答by Marlon Ayres

I've been having a huge problem exactly with this situation.

我在这种情况下遇到了一个大问题。

This is just to say that the last answer from Andrew Koester is the perfect answer I was looking for.

这只是说 Andrew Koester 的最后一个答案是我一直在寻找的完美答案。

In case you are passing multiple form entries from a jQuery form to PHP through the .ajax() call like this:

如果您通过 .ajax() 调用将多个表单条目从 jQuery 表单传递到 PHP,如下所示:

data: "name=" + name + "&message=" + message + ...

数据:“名称=”+名称+“&消息=”+消息+...

DON'T USE THIS METHOD, it will block the ampersand(&) character from being written by the user on any of the input fields of your form.

不要使用这种方法,它会阻止用户在表单的任何输入字段上写入&符号(&)字符。

Use this one instead as suggested by Andrew:

按照安德鲁的建议使用这个:

data: {"name": name, "email": email, "subject": subject, "comments": comments},

数据:{“姓名”:姓名,“电子邮件”:电子邮件,“主题”:主题,“评论”:评论},

This way the user can write any kind of special character whithout worrying a about conflicting with the ajax declaration.

这样用户可以编写任何类型的特殊字符而不必担心与 ajax 声明冲突。

回答by Michal

You can use a native javascript escape() function

您可以使用本机 javascript escape() 函数

In line 74

在第 74 行

data: : "&task_d=" + escape(task_d) + "" 

Alternatively, you could enclose your query string values in quotes

或者,您可以将查询字符串值括在引号中

data: : "&task_d='" + task_d + "'" 

回答by Jess

If you pass your dataparameter as a Javascript object, it will convert the characters for you (and IMO make the code look neater). So you should change your $.ajax call to the following:

如果您将data参数作为 Javascript 对象传递,它将为您转换字符(并且 IMO 使代码看起来更整洁)。因此,您应该将 $.ajax 调用更改为以下内容:

data: {"user_id": user_id, "time_r": time_r, "task_d": task_d, "p_id": p_id, "df": finished},

回答by user3613257

You could use 'encodeURIComponent' to accomplish the URL encoding for that component. I used this and validated with browsers IE, Firefox, and Chrome.

您可以使用“encodeURIComponent”来完成该组件的 URL 编码。我使用了它并通过浏览器 IE、Firefox 和 Chrome 进行了验证。