0

長さ n の配列をフォームに投稿したいと思います。これを処理する最良の方法は何ですか?

オブジェクトは、日付と行の配列を持つタイムシートです。

行には、期間、カテゴリ、およびメモ フィールドがあります。

私のフォームには日付フィールドがあり、開始するのは 1 行で、jQuery は必要に応じてさらに行を追加します。

どういうわけかブラケット表記を使用する必要があることはわかっていますが、2 つのオブジェクトがネストされていることを考えると、どうすればよいかわかりません。

FWIW、アプリケーションの終わりは Node.js と Express にあります。

<form id="timesheet" method="POST" action="/timesheets">
  <label>date for timesheet</label>
  <input type="date" name="date"/><br/>
  <ol id="timesheet-list">
    <li>
      <input type="number" name="hours" min="0" value="0" step=".25"/>
      <input type="text" name="category"/>
      <input type="text" name="details"/>
    </li>
  </ol>
  <input type="submit"/>
</form>

<a id="addItem" href="#">Add a new line item</a>

<script type="text/javascript">
  $('#addItem').click(function(e){
    e.preventDefault();
    $('#timesheet-list').append('<li><input type="number"> <input type="text"> <input type="text"></li>');
  });
</script>
4

2 に答える 2

0

JSONフォームにPOSTするようにデータをフォーマットしますか?

オブジェクトJSONは次のようになります。

// Entire object is a representation of a 'Timesheet'
{
  date: '8-8-2012',
  lines: [ // Array of objects, each storing a duration, catagory and note.
    {
      duration: 0,
      catagory: 'whatever',
      note: 'whatever'
    },
    { // Have as many of these as you please.
      duration: 123,
      catagory: 'another',
      note: 'moar notes'
    },
  ]
}

このオブジェクトを受け取ると、次のようにデータにアクセスできます。

data = getTimesheet(); // Assume this function gets the above JSON
data.date;
for(var i=0; i<data.lines.length; i++) {
    data.lines[i].duration;
    data.lines[i].catagory;
    data.lines[i].note;
}
于 2012-08-08T05:05:00.707 に答える