我选择了一些选项:
<select class="mat">
<option value="0">mat 1</option>
</select>
和表:
<table id="mat-table">
<tr>
<td>option 1</td>
<td>desc</td>
</tr>
<tr>
<td>option 2</td>
<td>desc</td>
</tr>
<tr>
<td>option 3</td>
<td>desc</td>
</tr>
</table>
我想用jquery复制文本'选项1','选项2'从这个表中选择选项并得到这个:
<select class="mat">
<option value="0">option 1</option>
<option value="1">option 2</option>
<option value="2">option 3</option>
</select>
请帮忙。
答案 0 :(得分:0)
试试这个:
$('#mat-table td:first-child').each(function(i) {
var $option = $('<option>', { value: i }).text(this.textContent);
$('.mat').append($option);
});
请参阅Fiddle
答案 1 :(得分:0)
$(document).ready(function(){
$('.mat').html('');
var i = 0;
$("#mat-table tr").each(function(){
$('.mat').append($('<option>', {
value: i,
text: $(this).find('td:nth-child(1)').html()
}));
i++;
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="mat">
<option value="0">mat 1</option>
</select>
<table id="mat-table">
<tr>
<td>option 1</td>
<td>desc</td>
</tr>
<tr>
<td>option 2</td>
<td>desc</td>
</tr>
<tr>
<td>option 3</td>
<td>desc</td>
</tr>
</table>
&#13;