繁体   English   中英

添加对象数组以形成对象

[英]add object array to form object


我有这样的html形式:

<form action="/create" method="POST">

    <input type="text" placeholder="title" name="title">
    <input type="text" placeholder="year" name="toYear">
    <input type="text" placeholder="genre" name="genre">
    <input type="text" placeholder="director" name="director">    
    <input type="url" placeholder="poster" name="poster">
    <textarea name="plot" placeholder="plot..." cols="30" rows="10"></textarea>            

    <div id="actors">
        <h5>Actors</h5>
        <button type="button" id="addActor">Add Actor</button>
    </div>

    <div id="ratings">
        <h5>Ratings</h5>
        <button type="button" id="addRating">Add Rating</button>
    </div>
<button type="submit">Add to Collection
</button>
</form>

而下面的我的脚本:

document.getElementById('addRating').addEventListener('click', function () {

    var ratingsElem = document.getElementById('ratings');

    var sourceElem = document.createElement("input");
    sourceElem.setAttribute('type', 'text');
    sourceElem.setAttribute('placeholder', 'Source');
    sourceElem.setAttribute('name', 'ratingsSource');

    var valueElem = document.createElement("input");
    valueElem.setAttribute('type', 'text');
    valueElem.setAttribute('placeholder', 'value');
    valueElem.setAttribute('name', 'ratingsValue');


    ratingsElem.appendChild(sourceElem);
    ratingsElem.appendChild(valueElem);

})

document.getElementById('addActor').addEventListener('click', function () {

    var ActorsElem = document.getElementById('actors');

    var actorElem = document.createElement("input");
    actorElem.setAttribute('type', 'text');
    actorElem.setAttribute('placeholder', 'Name');
    actorElem.setAttribute('name', 'actors');

    ActorsElem.appendChild(actorElem);
});

提交时一切顺利,但我想将Ratings值变成如下这样的评价对象数组:

"Ratings": [
{
  "Source": "Internet Movie Database",
  "Value": "8.8/10"
},
{
  "Source": "Rotten Tomatoes",
  "Value": "91%"
},
{
  "Source": "Metacritic",
  "Value": "92/100"
}
  ]

并将其附加到发送到服务器的表单数据中。 我该如何实现? ........................................

var formData = JSON.stringify($("#myForm").serializeArray());

您稍后可以在ajax中使用它。 或者,如果您不使用ajax; 将其放在隐藏的文本区域中并传递到服务器。 如果此数据通过常规格式数据作为json字符串传递,则必须使用json_decode对其进行解码。 然后,您将获得数组中的所有数据。

$.ajax({
  type: "POST",
  url: "serverUrl",
  data: formData,
  success: function(){},
  dataType: "json",
  contentType : "application/json"
});

参考: 如何使用html表单数据发送JSON对象

可以完成的方法很少,但这是最简单的方法

var rating = 0;

document.getElementById('addRating').addEventListener('click', function () {
  var ratingsElem = document.getElementById('ratings');

  var sourceElem = document.createElement("input");
  sourceElem.setAttribute('type', 'text');
  sourceElem.setAttribute('placeholder', 'Source');
  sourceElem.setAttribute('name', 'Ratings[' + rating + '][Source]');

  var valueElem = document.createElement("input");
  valueElem.setAttribute('type', 'text');
  valueElem.setAttribute('placeholder', 'value');
  valueElem.setAttribute('name', 'Ratings[' + rating +  '][Value]');

  ratingsElem.appendChild(sourceElem);
  ratingsElem.appendChild(valueElem);

 rating++;

});

如果您想在不添加变量评级的情况下执行此操作,则可以在源的每个新输入上计算其索引,然后添加具有相同索引的输入值...

我希望这可以帮到你...

这是我测试时的输出:

{
  "title": "",
  "toYear": "",
  "genre": "",
  "director": "",
  "poster": "",
  "plot": "",
  "Ratings": [
    {
      "Source": "1",
      "Value": "2"
    },
    {
      "Source": "2",
      "Value": "3"
    }
  ]
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM