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

时间:2021-10-05 23:58:33

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附加到val。

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

1 个解决方案

#1


33  

.serialize returns 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 name and value property:

而是使用.serializeArray [docs]创建数据的数组表示,然后将数据添加到它。数组的每个元素都是一个具有name和value属性的对象:

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);

#1


33  

.serialize returns 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 name and value property:

而是使用.serializeArray [docs]创建数据的数组表示,然后将数据添加到它。数组的每个元素都是一个具有name和value属性的对象:

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);