用JQuery如何遍历多个select的同时遍历每一个select的option?

syszj 2010-05-19 03:30:05
如题,还有一个问题就是如何在遍历option的时候取select的id,谢谢!
...全文
3836 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
skyaspnet 2011-02-24
  • 打赏
  • 举报
回复
还有一种写法:


$('select').each(function()
{
alert($(this).children("option:selected").val());
});
syszj 2010-05-19
  • 打赏
  • 举报
回复
多谢.
passself 2010-05-19
  • 打赏
  • 举报
回复

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<script src="jquery-1.3.2.js"></script>
<script>
$(function(){
$("select").each(function(){
$(this).children("option").each(function(){
alert($(this).text())//每一个option
alert($(this).text()+"属于id为"+$(this).parent("select").attr("id")+"的select");
});
});
});
</script>
</head>

<body>
<select id="test1">
<option>1111</option>
<option>2222</option>
<option>3333</option>
</select>
<select id="test2">
<option>aaaa</option>
<option>bbbb</option>
<option>cccc</option>
</select>
<select id="test3">
<option>一</option>
<option>二</option>
<option>三</option>
</select>
</body>
</html>

sunli555 2010-05-19
  • 打赏
  • 举报
回复
$("select").each(function(){
var myid=$(this).attr(id);
$(this).find("option").each(function(){
alert("此option的值为"+$(this).val()+",他上面的select的ID为"+myid);
});
});

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧