【问题标题】:JTemplates serializationJTemplates 序列化
【发布时间】:2010-01-15 15:11:23
【问题描述】:

我一直在使用 JTemplates 插件,我用它来创建一个通过模板绑定到 json 对象的表单。完美运行。不过,我想做的不是提交表单,而是将其重新序列化回其起源的 json 对象,并将其作为 json 字符串传递回控制器方法。将对象序列化回其原始格式的最佳方法是什么?

【问题讨论】:

    标签: jquery json jtemplate


    【解决方案1】:

    我使用 serializeObject 和 toJson 来完成这个。

    var yourForm = $('#formId');
    //Serialize form elements and make into json object
    var jsonObject = $.toJSON(yourForm.serializeObject());
    
    serializeObject (jquery)
    
    $.fn.serializeObject = function()
    {
       var o = {};
       var a = this.serializeArray();
       $.each(a, function() {
           if (o[this.name]) {
               if (!o[this.name].push) {
                   o[this.name] = [o[this.name]];
               }
               o[this.name].push(this.value || '');
           } else {
               o[this.name] = this.value || '';
           }
       });
       return o;
    };
    

    转JSON

    使用 json.js 库:https://github.com/douglascrockford/JSON-js

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多