我有一个循环遍历所有表数据。它非常适合从html标记(如“ <td>text here</td>
”)中提取“ 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中的第一个选择标签选择选项。
您可以使用val()
方法来获取所选选项的值。
var val = $(this).find("select").eq(0).val();
假设这$(this)
是td
您上下文中的元素,其中具有select元素。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句