簡體   English   中英

獲取表中td內的select標簽的select選項值

[英]Get selected option value of select tag inside a td in a table

我要在表中動態添加一個select選項標簽,而在提交時,我要對表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.
 }

但是,在將select標簽添加到table時,selected屬性不適用於任何選項。

如何獲得所有選定的國家

PS:這篇文章是通過手機發布的。 所以可能有錯字

您可以嘗試這樣的事情嗎?

$('#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>
});

那就是因為您正在對其調用html()函數,因此只能在jquery元素上調用find()

采用

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

或者您可以在單個命令中完成

 $('#tableid tbody tr').each(function() {   
       var value = $(this).find('td:eq(2) options:selected').val(); // return the whole select option as string

 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM