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

时间:2022-09-02 00:14:09

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.

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

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

5 个解决方案

#1


16  

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.

这个函数与htmlspecialchars()完全相同,除了htmlentites()之外,所有具有HTML字符实体的字符都被转换成这些实体。

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

如果您想要解码(相反),可以使用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()));

#2


7  

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 + ...

数据:“name = " +名称+“消息= " +信息+……

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},

数据:{"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声明冲突。

#3


4  

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 + "'" 

#4


3  

If you pass your data parameter 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:

如果您将数据参数作为Javascript对象传递,它将为您转换字符(在我看来,代码看起来更整洁)。所以你应该改变你的美元。ajax调用如下:

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

#5


1  

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进行了验证。

#1


16  

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.

这个函数与htmlspecialchars()完全相同,除了htmlentites()之外,所有具有HTML字符实体的字符都被转换成这些实体。

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

如果您想要解码(相反),可以使用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()));

#2


7  

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 + ...

数据:“name = " +名称+“消息= " +信息+……

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},

数据:{"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声明冲突。

#3


4  

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 + "'" 

#4


3  

If you pass your data parameter 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:

如果您将数据参数作为Javascript对象传递,它将为您转换字符(在我看来,代码看起来更整洁)。所以你应该改变你的美元。ajax调用如下:

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

#5


1  

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进行了验证。