[英]get jsonArray from table
我有一張桌子,需要建立json數組。 我需要表的1和4列中的值。
HTML
<table class="table table-striped" id="development_mapping">
<thead>
<tr>
<th>Visual Feature</th>
<th>Step</th>
<th>Output</th>
<th>Data Feature</th>
</tr>
</thead>
<tbody>
<tr><td>color</td>
<td><select id="sss"><option data-id="528092be144b4fbf65893404" selected="selected">first-step</option><option data-id="52809373144b4fbf6589340c">kmeans</option></select></td>
<td><select id="ooo"><option data-id="output" selected="selected">output</option></select></td>
<td><input id="value1" class="feature-execution"value="id"></td></tr></tbody>
</table>
這是我的解決方案,一個從表制作json數組的函數
JAVASCRIPT
var jsonArray = {};
$('#development_mapping').find('tr').each(function () {
var name = $(this).find('td:first').text();
jsonArray[name] = {
variable : $(this).find('td:eq(3)').text()
};
});
我已經做了,但不明白,為什么我從4列的值中得到" "
。 我的意思是,為什么變量中的變量總是得到" "
這是我的演示
您的選擇器: $('#development_mapping').find('tr')
正在選擇表中的所有 <tr>
標簽。 甚至是<thead>
中的那個! <thead>
沒有<td>
標記,因此就是" "
的來源。
嘗試這個:
$('#development_mapping').find('tbody tr').each(function () {
var name = $(this).find('td:first').text();
jsonArray[name] = {
variable : $(this).find('td:eq(3)').text()
};
});
您正在使用.text()
但第3個td
標簽中沒有實際的文本。
試試這個吧?
variable : $(this).find('td:eq(3) input').val()
您的代碼有2個問題。
<tr>
。 使用它來訪問輸入值
variable: $(this).find('td:eq(3) > input').first().val()
$.find()
將返回一個集合,您需要獲取該集合的第一個對象以供進一步使用。 另外,您需要搜索單元格內部的輸入,而不是單元格本身。
這是工作的小提琴: http : //jsfiddle.net/toshkaexe/7q3KP/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.