1

データ行の入力を必要とするバックボーン アプリに取り組んでいます (読み取り: オブジェクトの配列)。

私は次のように設定されたフォームを持っています:

<tr>
    <td><input type="text" name="items[][qty]"></td>
    <td><input type="text" name="items[][job_number]"></td>
    <td><input type="text" name="items[][description]"></td>
    <td><input type="text" name="items[][purchase_order]"></td>
</tr>
[...]

動的な行数で。

次の形式でデータを取得できるようにしたい:

{
    items: [
        {
            qty: [val],
            job_number: [val],
            description: [val],
            purchase_order: [val]
        },
        [...]
    ]
}

私が見つけた最も近い解決策はAaron Shafovaloffによるものですが、出力の配列をサポートしていません (オブジェクトのみ)。彼のコードを変更して必要なことを行うことはできますが、車輪を再発明する意味がないため、最初にここで質問することにしました。

私は自分のプロジェクトで jQuery と Underscore を使用しているので、それらのメソッドにアクセスできます。

4

2 に答える 2

3

https://github.com/serbanghita/formToObject を確認してください

var myFormObj = new formToObject('myFormId');
// console.log(myFormObj);
于 2013-09-23T19:36:47.843 に答える
1

私はこの方法で複数行のテキストボックスを取得しています:

    aItems = new Array();
    $("table tbody tr").each(function(){
        var $this = $(this);

        aItems.push({
            qty:$this.find('input[name="qty"]').val(),
            job_number: $this.find('input[name="job_number"]').val(),
            description:$this.find('input[name="description"]').val(),
            purchase_order:$this.find('input[name="purchase_order"]').val()
        });

    });

これはどう:

    aItems = new Array();
    $("table tbody tr").each(function(){
        var items = {};

        $(this).find('input').each(function(){
            items[$(this).attr('name')] = $(this).val();
        }
        aItems.push(items);

    });
于 2012-11-09T09:49:46.697 に答える