jquery从表列中获取所有值

Fis*_*ake 20 html jquery

我正在寻找一种方法,使用jQuery从表列填充具有唯一值的选择框.到目前为止,我的尝试都是徒劳的,但我对前端开发相对较新,所以我希望有人可以告诉我这个问题.

干杯.

red*_*are 47

这应该让你开始.请注意,我将单元格内容用于选项的值和文本.您可能需要更改此内容,但问题尚不清楚.

var items=[], options=[];

//Iterate all td's in second column
$('#tableId tbody tr td:nth-child(2)').each( function(){
   //add item to array
   items.push( $(this).text() );       
});

//restrict array to unique items
var items = $.unique( items );

//iterate unique array and build array of select options
$.each( items, function(i, item){
    options.push('<option value="' + item + '">' + item + '</option>');
})

//finally empty the select and append the items from the array
$('#selectId').empty().append( options.join() );
Run Code Online (Sandbox Code Playgroud)