serialize
returns a URL-encoded string containing the form fields. If you need to append to it, you do so using the standard URL-encoded string rules, e.g.:
var values = $("#frmblog").serialize();
values += "&content=" + encodeURIComponent(content_val);
(以上假设在serialize
调用之后,values
中总会有一个值;如果这不一定是真的,那么在添加之前,请根据values
是否为空来确定是否使用&
.)
Alternately, if you like, you can use serializeArray
and then add to the array and use jQuery.param
to turn the result into a query string, but that seems a long way 'round:
// 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);
Update: In a comment added later you said:
问题是,在序列化过程中,在‘Content’键中设置了一些默认值,所以我不能只附加一个新值,我必须更新其中已有的值."
That changes things. It's a pain to look for content
within the URL-encoded string, so I'd go with the array:
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);
You'd probably want to make this a reusable function.