【问题标题】:How to create json from Form?如何从表单创建 json?
【发布时间】:2015-08-31 19:36:46
【问题描述】:

我正在使用 Drag n Drop FormBuilder 来创建表单。

现在我想像这样创建这种形式的 JSON:

{
   "action":"hello.html",
   "method":"get",
   "enctype":"multipart/form-data",
   "html":[
        {
          "type":"p",
          "html":"You must login"
        },
        {
          "name":"username",
          "id":"txt-username",
          "caption":"Username",
          "type":"text",
          "placeholder":"E.g. user@example.com"
        },
        {
          "name":"password",
          "caption":"Password",
          "type":"password"
        }, 
       { 
         "name":"description",
         "caption":"Description",
         "type":"textarea"
       },
       {
          "name":"file",
          "caption":"Select File",
          "type":"file",
          "multiple":true,
          "id":"file2"
       },
       {
         "name":"number",
         "caption":"Your number",
         "type":"text"
       },
       {
         "name":"email",
         "caption":"Your email",
         "type":"text"
      },
      {
         "type":"radiobuttons",
         "name":"gender",
         "class":"",
         "options":{
             "male":{
                 "value":"male",
                 "caption":{
                    "html":"Male",
                  },
             },
            "female":{
                 "value":"female",
                 "caption":{
                     "html":"Female"}
            } 
         }
      },
      {
        "type":"checkboxes",
        "name":"color[]",
        "options":{
           "blue":{
              "value":"blue",
              "caption":{"html":"Blue"}
            },
           "red":{
               "value":"red",
               "caption":{"html":"Red"}
           },
          "black":{
             "value":"black",
             "caption":{"html":"Black",},
          }
       }
    },

    {
        "type":"submit",
       "value":"Login"
    }
  ]
}

我正在使用这个表单生成器http://getfuelux.com/formbuilder.html 来构建表单。

现在,当我将任何元素拖到区域时,我想要上面提到的那个字段的 json。如何创建表单的json

【问题讨论】:

    标签: javascript jquery json forms


    【解决方案1】:

    jQuery serializeArray() 可以满足您的需求。只需添加其他详细信息,例如 method 等。

    var jsonString = $("#myForm").serializeArray();
    //...add your stuff here like method, etc...
    

    【讨论】:

    • 你能举个例子吗
    • 没有提供所有信息,如占位符、标签、..这不像我在上面提到的那样
    猜你喜欢
    • 1970-01-01
    • 2013-12-13
    • 2015-11-22
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多