2017-05-09 52 views
0

我在表中動態地添加一個select選項標記,並在提交時迭代表tr以在td中查找元素。它將字符串本身的select標籤返回給我。在這裏,我不能夠得到選擇標籤獲取表中選擇標記的選項值

的選擇的選項

我的代碼是

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

但同時增加了選擇標籤表,選擇屬性,不適用於任何選項。

我怎樣才能得到所有選定的國家

P.S:這篇文章是通過手機。所以有可能是任何錯字

+0

你能分享HTML代碼嗎? –

+0

html代碼不是必需的,因爲選擇標籤正在使用javascript綁定 – Syed

回答

0

能否請您嘗試這樣的事:

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

非常好。正確對象,真愛。 – Syed

+0

最新問題? _你可以嘗試這樣的事情:_ – Satpal

+0

實際上,OP試圖從'html'字符串中獲取選定的國家。相反,我們可以做到這一點非常簡單,就像上面那樣,從長遠來看也很容易維護。 – vijayP

0

那是因爲你是在它調用該函數html(),你只能叫find() jQuery的元素

使用上

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

} 
相關問題