Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery serialize converts all spaces to plus

Currently, everywhere I use serialize I have to use it like this:

.serialize().replace(/\+/g,'%20'); 

otherwise any spaces in the form data will be coverted to +'s. Is there a setting that can make this the default.

like image 697
Mark Steggles Avatar asked Jun 14 '12 01:06

Mark Steggles


2 Answers

For fun, here's an alternative that doesn't use a temporary variable:

$.fn.serializeAndEncode = function() {     return $.map(this.serializeArray(), function(val) {         return [val.name, encodeURIComponent(val.value)].join('=');     }).join('&'); };  $("#formToSerialize").serializeAndEncode(); 
like image 171
russianmario Avatar answered Oct 09 '22 09:10

russianmario


I had to do the same thing. The solution Terry gave, with escape(), doesn't work. The = and & are getting encoded (we don't want that) and the + are still there.

What I did is creating my own function to serialize:

var QueryString = ""; $(selector).each(function(index) {     if(QueryString != "") QueryString += "&";     QueryString += $(this).get(0).id + "=" + encodeURIComponent( $(this).val() ); }); 
like image 24
Matt Roy Avatar answered Oct 09 '22 08:10

Matt Roy