-4

我有一个类似于以下内容的 html 页面。我想读取字段并创建一个分层的 javascript 对象(或 json)。怎么做?谢谢你的帮助。

<div class="t">
    <div>
        <div class="c">
            <input type="text"></input>
            <input type="text"></input>                
        </div>
    </div>
</div>
<div class="t">
    <div>
        <div class="t">
            <div>
                <div class="c">
                    <input type="text"></input>
                    <input type="text"></input>                
                </div>
            </div>
        </div>
    </div>
</div>

<div id="result"></div>

生成的 JSON 应如下所示:

"result":{
   "id":"",
   "ver":"",
   "name":"",
   "desc":"",
   "tdata":[
       //nested elements from class 't' in the order they are in html
       //each input element under class 'c' for each class 't'. 
    ]
}
4

1 回答 1

1

为了通过.t

$('.t').each(function() {

});

用于序列化input数据.c

$('.c input').serialize();

如果你想得到所有的孩子和孙子,你应该:

var nestedElements = [];
$("#wrapper").find("*").each(function() {
    nestedElements.push(this);
});
于 2013-06-22T12:22:47.653 回答