私は数日間この問題を解決しようとしてきました。私はAjaxによって提出される必要があるいくつかのデータを持っています。ダイナミックなので、フィールド数はわかりません。その上に、フィールドの名前は多次元であり、また動的です。別の大きい方の配列に要素の未知数とdata[images][0][src]
、data[images][0][alt]
、data[images][0][description]
、data[images][1][src]
、data[images][1][alt]
、data[images][1][description]
があるかもしれない一方で、例えば一方が名前とdata[title]
との入力を有するかもしれません。私の問題は、データの構造を維持しながら、Ajax経由で送信できるオブジェクトを取得する必要があることです。mutlidimensional入力をJavaScriptオブジェクトに変換する
データをシリアル化しようとしましたが、文字列はname = value
です。私もserialise配列を試したが、それはちょうど私に[name, value]
の配列を与える。私はそれを分割する正規表現を使用して手動でオブジェクトを生成するために管理しましたが、結果のオブジェクトを一緒にマージする方法を見つけることができません。私のコードの最新バージョンは次のとおりです。
$('.modal-content').find('input[name^="data"]').each(function() {
var found = $(this).attr('name').match(re).reverse();
var temp = $(this).val();
$.each(found, function()
{
str = this.substr(1, this.length - 2);
var t = {};
t[str] = temp;
temp = t;
});
data = $.each({}, data, temp);
});
残念ながら、それはそれらをマージしていない、それは一時にあるものとのデータであるものを上書きします。データがdata.images[0].src = "mysrc"
で、テンポがdata.images[0].alt = "myalt"
の場合は、data.images[0].alt = "myalt"
となり、srcはもう存在しません。
これを行うには単純な方法が必要ですが、現時点ではこれを行うには複雑な方法をとることさえあります。誰かがこれで私を助けてくれますか?
HTML自体はどのように見えますか?動的ですが、それはまったく設定パターンに従っていますか? –
@RoryMcCrossan唯一の一般的な要因はID付きのdivにあり、それらはすべてパターン 'name =" data [x] [y] [z] 'に従うということです。ここで、xyzは動的であり、いくつかのテキスト入力、その他のラジオボタン、いくつかの選択と他のテキストエリア(私は入力atmをしていることを知っている、入力のために働いたらコードを調整する) – Styphon
実際のHTMLはかなり長いので、 http://pasted.co/0643034a – Styphon