将对象数组添加到表单对象

时间:2018-01-26 10:33:53

标签: javascript html json forms


我有一个像这样的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": [
{
  "Source": "Internet Movie Database",
  "Value": "8.8/10"
},
{
  "Source": "Rotten Tomatoes",
  "Value": "91%"
},
{
  "Source": "Metacritic",
  "Value": "92/100"
}
  ]

并将其附加到表单发送到服务器的数据。我怎么能实现这个目标? ........................................

2 个答案:

答案 0 :(得分:1)

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

您可以稍后在ajax中使用它。或者如果你没有使用ajax;把它放在隐藏的textarea中并传递给服务器。如果此数据通过普通表单数据作为json字符串传递,则必须使用json_decode对其进行解码。然后,您将获取数组中的所有数据。

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

参考:How to send a JSON object using html form data

答案 1 :(得分:1)

这样做的方法很少,但这里是最简单的一种

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++;

});

如果你想在不添加变量 rating 的情况下这样做,在源的每个新输入上你可以计算他的索引,而不是用相同的索引添加输入值......

我希望这有用......

我测试时输出:

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