javascript 如何使用 jquery 序列化对值进行编码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6653878/
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-10-25 21:24:52  来源:igfitidea点击:

How to encode value with jquery serialize?

javascriptjqueryhtml

提问by Gihan Lasita

I tried to encode all values like

我尝试对所有值进行编码,例如

encodeURIComponent($("#customer_details").serialize());

and that doesn't work as expected.

这并没有按预期工作。

Is there way to get all elements on form and use encodeURIComponentto encode each value?

有没有办法获取表单上的所有元素并用于encodeURIComponent对每个值进行编码?

回答by user113716

It should already be encoded when using the serialize()[docs]method.

使用serialize()[docs]方法时应该已经对其进行了编码。

From the docs:

从文档:

The .serialize()method creates a text string in standard URL-encoded notation.

.serialize()方法以标准 URL 编码表示法创建文本字符串。

Example:http://jsfiddle.net/WArUG/

示例:http : //jsfiddle.net/WArUG/

If you want to represent a space with a %20instead of a +, you'll need to do a .replace(/\+/g,'%20').

如果你想用 a%20而不是 a来表示一个空间+,你需要做 a .replace(/\+/g,'%20')