迭代表jQuery中的选择框

时间:2012-07-27 18:49:46

标签: javascript jquery html

我在表单中添加了行选项,用户可以根据需要添加任意数量的行,然后通过ajax一次性提交。

每行包含选择框和一堆文本框。

正确提交了文本框,但我的表单只是在所有其他行的第一行中提交了选择框值,尽管值不同。

正确解释了我的问题(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等其他浏览器中运行良好。

循环工作正常,因为我能够获得其他输入框值。

我也尝试过没有id属性的html选择boxe,但也没有用。

我真的需要别人的帮助。

提前感谢天才。

标记。

2 个答案:

答案 0 :(得分:2)

我认为你正在寻找这个:

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

答案 1 :(得分:0)

假设每行只有一个选择框,这应该有效:

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

如果您有更复杂的标记,并且您只想从每行的第一个单元格中获取值,则可以将选择器更改为

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