2012-07-27 42 views
0

我在表單中添加了行選項,用戶可以根據需要添加儘可能多的行,然後通過ajax一次提交所有行。遍歷表中的選擇框jQuery

每行都包含選擇框和一堆文本框。

正確提交了文本框,但我的表單僅在所有其他行的第一行中提交了選擇框值,但值不同。

在正確地(hoepfully)解釋了我的問題,這裏是爲選擇框HTML代碼代碼:

<td><span id="xyz"><select id="whatever"> 
<option value="one">ONE</option> 
<option value="two">TWO</option> 
<option value="three">THREE</option> 
</select> 
</span></td> 

和下面的代碼不同的代碼版本的jQuery來拿起所有的選擇框值:

$("#table tbody tr").each(function(){ 

var select = $(this).find("td:eq(0)").find("select#whatever).val(); 
//etc 
} 

$("#table tbody tr").each(function(){ 

var select = $("select#whatever).val(); 
//etc 
} 

$("#table tbody tr").each(function(){ 

var select = $(this).find("td:eq(0)").find("select#whatever option:selected").val(); 
//etc 
} 

$("#table tbody tr").each(function(){ 

var select = $(this).find("td:eq(0)").find("select#whatever").val(); 
//etc 
} 

$("#table tbody tr").each(function(){ 

var select = $(this).find("td:eq(0)").find("select").val(); 
//etc 
} 

$("#table tbody tr").each(function(){ 

var select = $(this).find("td:eq(0).xyz").find("select#whatever").val(); 
//etc 
} 

$("#table tbody tr").each(function(){ 

var select = $(this).find("td:eq(0).dropdown select#whatever").val(); 
//etc 
} 


$("#table tbody tr").each(function(){ 

var select = $(this).find("td:eq(0)").find("select#whatever").val(); 
//etc 
} 

$("#table tbody tr").each(function(){ 

var select = $("select#whatever option:selected").val(); 
//etc 
} 

$("#table tbody tr").each(function(){ 

var select = $("select option:selected").val(); 
//etc 
} 

首先是代碼的各種版本,如果我沒有記錯,我在IE 8已經嘗試沒有成功。

他們在IE 9和Firefox等其他瀏覽器中工作正常。

循環工作正常,因爲我能夠獲得其他輸入框的值。

我也試過html選擇沒有id屬性的boxe,但也沒有工作。

我真的需要有人的幫助。

感謝天才提前。

Mark。

回答

1

我認爲你正在尋找這樣的:

var selectValue = $(this).find("td:eq(0)").find("select option:selected").val();

0

假設只有一個每行選擇框,這應該工作:

var values = $("#table select").map(function(){ 
    return $(this).val(); 
}).get(); 

如果你有更復雜的標記,並有您只想從每行的第一個單元格獲取值,則可以將選擇器更改爲

$('#table td:first-child select')