0

I am trying to figure out how to collect the field values of a form which updates another section on the page to create an object each time it is submitted. I can create an object each time by using .serializeArray() but i want to be able to create an object with a new name each time it submits. I was thinking of using each() to create a new name with i=1 and i++ each time but not sure if this is even the correct way to go about getting the results.

4

1 回答 1

0

This is _.uniqueId from underscore. Just set the object name to the result of the function and you'll always have a unique name.

  var idCounter = 0;
  var uniqueId = function(prefix) {
    var id = ++idCounter + '';
    return prefix ? prefix + id : id;
  };


var formvalues = {};
forvalues[uniqueId("data")] = {a:1,b:2};//or whatever ur form serializes to
console.log(formvalues); // {data1: {a:1,b:2}}
于 2013-10-29T20:13:50.610 回答