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);
(The above assumes there will always be one value in values
after the serialize
call; if that's not necessarily true, determine whether to use &
based on whether values
is empty before you add to it.)
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:
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"
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…