获取html表Jquery中行的最后一列中下拉列表的所选选项值
我有一个大约有30列的表,我想为每行中出现的下拉列表获取所选选项。我目前有下面的,但它返回null或未定义获取html表Jquery中行的最后一列中下拉列表的所选选项值,jquery,html,Jquery,Html,我有一个大约有30列的表,我想为每行中出现的下拉列表获取所选选项。我目前有下面的,但它返回null或未定义 <table class="table table-hover"> <thead> <tr> <th>Firstname</th> <th>Lastname</th> <th>Email</th> <th>Invite Status&l
<table class="table table-hover">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>Invite Status</th>
</tr>
</thead>
<tbody id="invitees">
<tr>
<td>John</td>
<td>Doe</td>
<td>john@example.com</td>
<td><select class="form-control"><option value="Invitee" selected="selected">Invitee</option><option value="Alternate">Alternate</option></select></td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>john@example.com</td>
<td><select class="form-control"><option value="Invitee" selected="selected">Invitee</option><option value="Alternate">Alternate</option></select></td>
</tr>
<!-- and so on ... Built Dynamically -->
</tbody>
或
我是否遗漏了一些明显的东西,或者这是不能做到的?这似乎应该可以工作…看起来您的表缺少被邀请者的id选择器。一旦有了这个值,就可以在表行中循环,获取最后一个td-select值,将其推送到一个数组中,然后将其加入到一个逗号分隔的列表中。此外,您缺少所选内容的结束
var invite_status=[];
$document.readyfunction{
$'invitees tbody tr'。每个函数{
var selection=$this.find'td:last option:selected'.val;
控制台。日志选择;
邀请_状态。推送选择;
};
console.loginvite_statuses.join',';
};
名字
姓氏
电子邮件
邀请状态
约翰
雌鹿
john@example.com
候补
受邀者
约翰
雌鹿
john@example.com
候补
受邀者
您可以直接参考选择框。以下解决方案提供了每个选择框中的选定项。检查控制台值 var-rst=; $'select'。每个函数{ rst+=$this.find'option:selected'。文本+; }; console.logrst; 名字 姓氏 电子邮件 邀请状态 约翰 雌鹿 john@example.com 被邀请者 约翰 雌鹿 john@example.com 被邀请者
你能分享html吗?@DeepuReghunath当然可以。谢谢你抓住我丢失的结束标记,但我不想要Id,因为这需要为每个下拉列表和循环时生成唯一的Id。修复丢失的标记后,结果相同。您的表缺少ID选择器,而不是select标记。您的原始JS代码引用了invitees tr,这表明您的表使用的是id invitees,除非我遗漏了什么。您是对的,抱歉。这是一个复制粘贴错误。。。它确实存在。我将把它添加到问题的代码中。不用担心,我只是想确保引用的是您的完整代码。上面的答案同样有效!我没有想到这种方法!非常感谢。
$('#invitees tr').each(function () {
invite_statuses += $('td:last select').find('option:selected').val() + ",";
})
$('#invitees tr').each(function () {
invite_statuses += $('td:last select').find('option:selected').text() + ",";
})