jQuery 我可以将数据添加到已经序列化的数组中吗?

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

Can I add data to an already serialized array?

jquery

提问by NaN

I am using ckeditor and would like to serialize the textarea data along with all of the other elements. Is this possible?

我正在使用 ckeditor 并希望将 textarea 数据与所有其他元素一起序列化。这可能吗?

I would like to append the taData to vals if possible.

如果可能,我想将 taData 附加到 vals。

var vals = $("#post").find('input,select').serialize();
var taData = CKEDITOR.instances.ta1.getData();

回答by Felix Kling

.serializereturns a string, so you can always modify the string, but I would not recommend this, string manipulation can get messy.

.serialize返回一个字符串,因此您可以随时修改该字符串,但我不建议这样做,字符串操作可能会变得混乱。

Instead, use .serializeArray[docs]to create an array representation of the data and then add the data to it. Each element of the array is an object with a nameand valueproperty:

相反,使用.serializeArray[docs]创建数据的数组表示,然后将数据添加到其中。数组的每个元素都是一个具有nameandvalue属性的对象:

var vals = $("#post").find('input,select').serializeArray();
vals.push({name: 'nameOfTextarea', value: CKEDITOR.instances.ta1.getData()});

All jQuery Ajax methods will understand this structure and serialize the data properly. In case you want to create a serialized string (just like .serialize), you can pass the array to $.param[docs]:

所有 jQuery Ajax 方法都会理解这种结构并正确地序列化数据。如果你想创建一个序列化的字符串(就像.serialize),你可以将数组传递给$.param[docs]

var query_string = $.param(vals);