jquery从表列中获取所有值 [英] jquery get all values from table column

查看:127
本文介绍了jquery从表列中获取所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法,使用jQuery从表列中填充具有唯一值的选择框。到目前为止,我的尝试是徒劳的,但我对前端开发相对较新,所以我希望有人能给我看看。

I'm looking for a way to populate a select box with unique values from a table column using jQuery. So far my attempts have been futile but I am relatively new to front end development so I'm hoping someone can show me the light.

干杯。

推荐答案

这应该让你开始。请注意,我将该单元格内容用于该选项的值和文本。你可能需要改变这一点,但这个问题还不清楚。

This should get you started. Note that I use the cell contents for both the value and the text of the option. You may need to change this but it is unclear from the question.

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() );

这篇关于jquery从表列中获取所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆