Couldn't find this anywhere, maybe someone knows or can make a suggestion.
I had a form with lots of <inputs>
, I wanted to send that form with jQuery $.ajax
functionality, so I did $('#myform').serialize()
and send this as json.
Now my form is more advanced and has HTML5 data-
attributes, that I want to send too, but .serialize()
doesn't see them.
I tried putting them in <form>
tag, <input>
tags - nothing works.
What is the best practice to grab them and send with all the form data? I know about .serializeArray()
, but how do I get all of the data-
attributes that my <form>
tag has attached serialized?
Alternatively, you can also use the jQuery data() method (jQuery version >= 1.4. 3), to get the data-attribute of an element using the syntax like $(element). data(key) . That means in the above example to get the data-id using data() method you can use the statement like $(this).
The serialize() method creates a URL encoded text string by serializing form values. You can select one or more form elements (like input and/or text area), or the form element itself. The serialized values can be used in the URL query string when making an AJAX request.
To serialize a FormData object into a query string, pass it into the new URLSearchParams() constructor. This will create a URLSearchParams object of encoded query string values. Then, call the URLSearchParams. toString() method on it to convert it into a query string.
Here's how it can be done. It might not be the best way, but it works the way it should work.
http://jsfiddle.net/Bvzqe/12/
HTML:
<form id="frm" data-id="123" data-list[one]="first" data-list[two]="second">
The serialization:
var form = $('#frm');
var dataarr = new Array();
for(var i in form.data()) {
var subarr = new Array();
subarr['name'] = i;
subarr['value'] = form.data()[i];
dataarr.push(subarr);
}
var serialized = $.param(form.serializeArray().concat(dataarr));
It even allows you to have arrays of data-
attributes such as
data-list[one]="first" data-list[two]="second"
URL encoded it may seem wrong, as it escapes square brackets, but I've tested this on the server side - it parses everything exactly as it should.
This is only for those that don't want to use <input type="hidden">
If at all possible you should store your additional values as hidden
input fields (one per value) rather than as meta-data on other input fields. They'll then get serialized automatically as part of the form.
I won't write a serializer for you, as I think it's a bad idea. If you insist on sending the values to the browser as data-
fields you could do this, though, to convert those data-
fields into hidden
inputs.
$('#myform:input').each(function() {
var input = this;
$.each($(input).data(), function(key, value) {
$('<input>', {type: hidden, name: key, value: value}).insertAfter(input);
});
});
Hey presto, hidden input fields that'll be automatically serialized!
Be aware that jQuery also uses .data()
to store things like events. To avoid iterating over those objects you'd have to use the native DOM functions to retrieve the data-
attributes, and not any data-related properties that have been stored on the elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With