Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make $.serialize() take into account those disabled :input elements?

It seems by default disabled input elements are ignored by $.serialize(). Is there a workaround?

like image 370
fms Avatar asked Oct 02 '22 15:10

fms


People also ask

What is the use of serialize () method in jQuery?

jQuery serialize() Method 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.

How do you serialize data in JavaScript?

In JavaScript, for example, you can serialize an object to a JSON string by calling the function JSON. stringify() . CSS values are serialized by calling the function CSSStyleDeclaration. getPropertyValue() .

How do you serialize data in a form?

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.


2 Answers

Temporarily enable them.

var myform = $('#myform');

 // Find disabled inputs, and remove the "disabled" attribute
var disabled = myform.find(':input:disabled').removeAttr('disabled');

 // serialize the form
var serialized = myform.serialize();

 // re-disabled the set of inputs that you previously enabled
disabled.attr('disabled','disabled');
like image 253
user113716 Avatar answered Oct 23 '22 14:10

user113716


Use readonly inputs instead of disabled inputs:

<input name='hello_world' type='text' value='hello world' readonly />

This should get picked up by serialize().

like image 102
Andrew Avatar answered Oct 23 '22 14:10

Andrew