我正尝试在 AJAX 中提交我的表单,因此我必须对数据进行序列化 ()。但是我正在使用 fckEditor
并且 jQuery 不知道如何处理它,所以在序列化之后,我试图手动修改值,但到目前为止没有运气......任何想法
if(content_val!=""){
var values = $("#frmblog").serialize();
values.content = content_val; //content_val is the manually fetched data which I am trying to insert into the serialized content.
alert(content_val); alert(values);
}
最佳答案
serialize
返回包含表单字段的 URL 编码字符串。如果您需要附加到它,您可以使用标准的 URL 编码字符串规则,例如:
var values = $("#frmblog").serialize();
values += "&content=" + encodeURIComponent(content_val);
(上面假设在 serialize
调用之后,values
中总是有一个值;如果这不一定正确,请确定是否使用 &
根据 values
在添加之前是否为空。)
或者,如果您愿意,可以使用 serializeArray
然后添加到数组并使用 jQuery.param
将结果转换为查询字符串,但这似乎还有很长的路要走:
// You can also do this, but it seems a long way 'round
var values = $("#frmblog").serializeArray();
values.push({
name: "content",
value: content_val
});
values = jQuery.param(values);
更新:在稍后添加的评论中,您说:
The problem is, there is some default values being set in the 'content' key during the serilization process, so I can't just attach a new value, I have to update the one already in it"
这改变了一切。在 URL 编码的字符串中查找 content
很痛苦,所以我会使用数组:
var values, index;
// Get the parameters as an array
values = $("#frmblog").serializeArray();
// Find and replace `content` if there
for (index = 0; index < values.length; ++index) {
if (values[index].name == "content") {
values[index].value = content_val;
break;
}
}
// Add it if it wasn't there
if (index >= values.length) {
values.push({
name: "content",
value: content_val
});
}
// Convert to URL-encoded string
values = jQuery.param(values);
您可能希望使它成为一个可重用的函数。
关于jquery - 如何在 jQuery 中修改序列化的表单数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5075778/