Sunday, June 24, 2018

jquery - Serialize form data to JSON

I want to do some pre-server-validation of a form in a Backbone.js model. To do this I need to get the user input from a form into usable data.
I found three methods to do this:




  1. var input = $("#inputId").val();

  2. var input = $("form.login").serialize();

  3. var input = $("form.login").serializeArray();



Unfortunately, none of the provide a good reabable and developable JSON object which I require. I already looked through several questions on Stack Overflow, but I found only some extra libraries.



Doesn't Underscore.js, the current jQuery or Backbone.js provide a helper method?



I can't imagine there is no request for such a function.



HTML






JavaScript



var formData = $("form.login").serializeObject();
console.log(formData);


Outputs



{
"name": "dev.pus",
"pass": "1234"
}


Backbone.js model



var user = new User(formData);
user.save();

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...