The serialize() method in jQuery is used to collect form data and convert it into a URL-encoded query string. It is commonly used when submitting form data using AJAX.
The serialize() method gathers input values from a form and encodes them as name-value pairs.
// Syntax of jQuery serialize()
$(selector).serialize();
// Serialize form data on button click
$("#btn").click(function () {
var data = $("#myForm").serialize();
console.log(data);
});
If a form has fields like name=John and age=25, the serialized output will be:
name=John&age=25
Click the button to see output
// Display serialized data in the output box
$("#btn").click(function () {
$("#resultText").text($("#myForm").serialize());
});
name attribute$.ajax() for best resultsserialize() vs serializeArray()