获取表中 td 内选择标记的选定选项值

Sye*_*yed 1 javascript jquery

我正在动态地在表中添加一个选择选项标签,并在提交时迭代表 tr 以查找 td 中的元素。它将选择标签作为字符串本身返回给我。在这里我无法获得选择标签的选定选项

我的代码是

$('#tableid tbody tr').each(function() {   
       var countryTag = $(this).find('td:eq(2)').html(); // return the whole select option as string
       var selCntry = countryTag.find('option:selected').val(); // this is throwing error.
 }
Run Code Online (Sandbox Code Playgroud)

但是在将 select 标签添加到 table 时, selected 属性对任何选项都不可用。

我怎样才能获得所有选定的国家

PS:这篇文章是通过手机制作的。所以可能有错别字

vij*_*ayP 6

你能不能试试这样的事情:

$('#tableid tbody tr').each(function() {   
    var tdObject = $(this).find('td:eq(2)'); //locate the <td> holding select;
    var selectObject = tdObject.find("select"); //grab the <select> tag assuming that there will be only single select box within that <td> 
    var selCntry = selectObject.val(); // get the selected country from current <tr>
});
Run Code Online (Sandbox Code Playgroud)