Javascript 将表值转换为JSON值
我有一张桌子。 HTMLJavascript 将表值转换为JSON值,javascript,jquery,html,json,Javascript,Jquery,Html,Json,我有一张桌子。 HTML <div class="row-fluid"> <table class="table table-striped" id="table-visual-features"> <thead> <tr> <th>Visual Feature</th> <th>Type [curre
<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>
我取行中第一个单元格的值,它是“A11”。
行中第二个单元格的值为“数值[1..30]”。
行中第三个单元格的值为“要映射到S1轴的值”。
我们先进入
features: {
"A11": {"type_feature": "Numeric [1..30]", "description_feature": "Values to be mapped to the S1-axis"}}
这是我的护照
请帮我转换数值。提前谢谢试试这个
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);
试试这个
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);
此代码将在行上循环,并将数据提取到features对象中:
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);
JSFIDLE示例-此代码将在行上循环,并将数据提取到features对象中:
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);
JSFIDLE示例-不清楚您想说什么?FTI:这不是JSON值。它是一个JavaScript对象。JSON是数据的字符串表示形式,类似于JavaScript语法。如果不是字符串,就不是JSON。@Subhash:他想将HTML表“转换”为JavaScript对象。你试过什么了吗?你被困在哪里了?我已经更新了我的主题不清楚你想说什么?FTI:那不是JSON值。它是一个JavaScript对象。JSON是数据的字符串表示形式,类似于JavaScript语法。如果不是字符串,就不是JSON。@Subhash:他想将HTML表“转换”为JavaScript对象。你试过什么了吗?你被困在哪里了?我已经更新了我的主题,我很感激被接受的答案,它可能应该交给安东,因为他在我之前两分钟发布了与我基本相同的答案。我很感激被接受的答案,它可能应该交给安东,因为他在两分钟前发布了与我基本相同的答案在我之前,耶呜呜呜!谢谢你的解决方案。这对我很有帮助!!!啊,那是《行动计划》的第一次编辑,tds被称为S1A@RichardDaltonyahooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo!谢谢你的解决方案。这对我很有帮助!!!啊,这是《行动计划》的第一次编辑,tds被称为S1A@richarddalton