如题,还有一个问题就是如何在遍历option的时候取select的id,谢谢!

解决方案 »

  1.   

    $("select").each(function(){
           var myid=$(this).attr(id);
           $(this).find("option").each(function(){
                    alert("此option的值为"+$(this).val()+",他上面的select的ID为"+myid);
           });
    });
      

  2.   


    <!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>
      

  3.   

    还有一种写法:
    $('select').each(function()
       {
        alert($(this).children("option:selected").val());
       });