0

Primarily for layout purposes I would prefer to use <div> tags in my views with contenteditable="true" for editing a model's attributes. That said, I've found it difficult to find a reliable solution for serializing these elements' values for saving to the model.

Is there a reliable way to serialize the contents of a groups of <div contenteditable="true"> elements?

4

1 回答 1

2

如果容器中有内容可编辑的 div,则可以遍历容器的子项并获取每个值并将其存储在对象中:

handleForm: function () {
  var formData = {};
  var value;

  $('#container').children('div[contenteditable]').each(function() {
    value = this.innerHTML;
    if (value) {
      formData[this.id] = value;
    }
  });

  // do stuff with formData object 
  this.model.create(formData); /* or this.collection.create(formData);
}
于 2013-09-21T16:09:11.107 回答