我有一张桌子。 的 HTML
<div class="row-fluid">
<table class="table table-striped" id="table-visual-features">
<thead>
<tr>
<th>Visual Feature</th>
<th>Type [currently not used]</th>
<th>Description</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr><td>A11</td><td>Numeric [1..30]</td><td>Values to be mapped to the S1-axis</td><td><button class="action-change">edit</button> <button class="btn btn-mini btn-danger action-remove-visual-feature">remove</button></td></tr>
<tr><td>A21</td><td>Numeric [1..10 ]</td><td>Values to be mapped to the S2-axis</td><td><button class="action-change">edit</button> <button class="btn btn-mini btn-danger action-remove-visual-feature">remove</button></td></tr>
<tr><td>A31</td><td>Numeric [1..20 ]</td><td>Values to be mapped to the S3-axis</td><td><button class="action-change">edit</button> <button class="btn btn-mini btn-danger action-remove-visual-feature">remove</button></td></tr>
</tbody>
</table>
</div>
我想将我的值从表转换为此表单
var features = {
features: {
"A11": {"type_feature": "Numeric [1..30]", "description_feature": "Values to be mapped to the S1-axis"},
"A21": {"type_feature": "Numeric [1..10]", "description_feature": "Values to be mapped to the S2-axis"},
"A31": {"type_feature": "Numeric [1..20]", "description_feature": "Values to be mapped to the S3-axis"}
}
};
我取行中第一个单元格的值,它是“A11”。 行中第二个单元格的值为“Numeric [1..30]”。 行中第三个单元格的值是“要映射到S1轴的值”。 我们先进入
features: {
"A11": {"type_feature": "Numeric [1..30]", "description_feature": "Values to be mapped to the S1-axis"}}
这是我的jsfiddle http://jsfiddle.net/7q3KP/
请帮我转换价值。提前谢谢
答案 0 :(得分:4)
试试这个
var features = {features:{}};
$('tr:not(:first)').each(function () {
var name = $(this).find('td:first').text();
features["features"][name] = {
"type_feature": $(this).find('td:eq(1)').text(),
"description_feature": $(this).find('td:eq(2)').text()
}
});
console.log(features);
答案 1 :(得分:2)
此代码将遍历行并将数据提取到您的功能对象中:
var features = { features: {}};
$('#table-visual-features tbody tr').each(function() {
var cells = $(this).find('td');
features.features[cells.eq(0).text()] = {
"type_feature": cells.eq(1).text(),
"description_feature": cells.eq(2).text()
};
});
console.log(features);
JSFiddle示例 - http://jsfiddle.net/Bdv4L/