我有一个遍历所有表数据的循环。它非常适合从像“<td>text here</td>
”这样的html标记中提取“text here”字符串,但我在某些单元格中嵌套了元素,例如“<td><select><option>1</option><option>2</option><option>3</option></select><label>some other string</label></td>
”
这是我试图用来从select元素中获取所选选项的代码。
$(this).children().eq(0).find("option:selected");
select元素是嵌套在<td></td>
内的第一个元素,这就是为什么它是children().eq(0)
$(this)
是<td>...</td>
我希望.find("option:selected");
能够检索所选的选项,但它不是有效的代码。
以下是常见<td>
标记包含的结构示例:
<td><select><option>1</option><option>2</option><option>3</option></select><label>some other string</label></td>
我想在td中为第一个选择标签选择选项。
答案 0 :(得分:0)
您可以使用val()
方法获取所选选项的值。
var val = $(this).find("select").eq(0).val();
假设$(this)
是您的上下文中的td
元素,其中包含select元素。