To serialize form data into a query string using jQuery, you can use the .serialize()
or .serializeArray()
methods. Here's an example:
HTML form:
<form id="myForm">
<input type="text" name="name" value="John">
<input type="email" name="email" value="[email protected]">
<input type="number" name="age" value="25">
</form>
JavaScript:
var formData = $('#myForm').serialize();
console.log(formData);
Output:
name=John&email=john%40example.com&age=25
In the example above, the serialize()
method is used to serialize form data into a query string. The resulting formData
string can then be used in an AJAX request or appended to a URL as query parameters.