我有一个表,每个表行都有许多'选择'。
我需要遍历每个表行并将'options'附加到数组中的选项。目前,只有表中的第一个“select”填充了数组中的数据。
我有以下HTML:
<table id="myTable">
<thead>
<tr>
<th>Code</th>
<th>Date</th>
<th>Description</th>
<th>Percentage</th>
</tr>
</thead>
<tbody>
<tr class="RepeatingSection">
<td>
<select id="InputCode"></select>
</td>
<td>
<input type="date" name="Date" id="DateInput" required="required" />
</td>
<td>
<input type="text" id="DetailsInput" name="Details" size="35" required="required" />
</td>
<td>
<input type="number" id="percentageInput" max="100" value="100" />%</td>
</tr>
<tr class="RepeatingSection">
<td>
<select id="InputCode"></select>
</td>
<td>
<input type="date" name="Date" id="DateInput" required="required" />
</td>
<td>
<input type="text" id="DetailsInput" name="Details" size="35" required="required" />
</td>
<td>
<input type="number" id="percentageInput" max="100" value="100" />%</td>
</tr>
<tr class="RepeatingSection">
<td>
<select id="InputCode"></select>
</td>
<td>
<input type="date" name="Date" id="DateInput" required="required" />
</td>
<td>
<input type="text" id="DetailsInput" name="Details" size="35" required="required" />
</td>
<td>
<input type="number" id="percentageInput" max="100" value="100" />%</td>
</tr>
</tbody>
我目前有以下JavaScript:
var array;
var i;
var option;
var select;
array = [001, 002, 003, 004];
for (i = 0; i < array.length; i++) {
option = document.createElement("option");
option.value = array[i];
option.text = array[i];
select = document.getElementById("InputCode");
$('#myTable .RepeatingSection').each(function() {
$(this).find('#InputCode').each(function() {
select.appendChild(option);
})
})
select.appendChild(option);
}
另请参阅我的fiddle
任何帮助将不胜感激。希望这篇文章有道理。
答案 0 :(得分:1)
尝试使用以下JavaScript代码:
var array;
var i;
var option;
array = [001, 002, 003, 004];
for (i = 0; i < array.length; i++) {
option = document.createElement("option");
option.value = array[i];
option.text = array[i];
$('#myTable .RepeatingSection').find( "select[id=InputCode]" ).append(option);
}