jQuery("select[@name=grade0] option").each(function(){ 
   if(jQuery(this).val() ==grade){alert(jQuery(this).val());
   jQuery(this).attr("selected","selected"); 
   } 
   }); jQuery("select[@name=subject0] option").each(function(){ 
   if(jQuery(this).val() ==subject){ alert(jQuery(this).val());
   jQuery(this).attr("selected","selected"); 
   } 
   });
//jQuery(this).attr("selected","selected"); 我初步怀疑是这个this的问题,因为我把其中一个遍历删除了,运行出来,还是两个显示的相同的,这个应该怎么处理呢?

解决方案 »

  1.   

    首先,这个,请您把jQuery都替换成$,看着别扭。
      

  2.   

    [@name=grade0] 写法比较老的,你jquery版本太低,给你的参考:
    <!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>无标题文档</title>
    <script src='http://code.jquery.com/jquery-latest.js'></script>
    <script type="text/javascript">
    $(function(){
    $("select[name=grade0]").val('grade')
    })</script>
    <style></style>
    </head><body>
    <select name='grade0'>
    <option>aa</option>
    <option>bb</option>
    <option>grade</option>
    </select>
    </body>
    </html>
      

  3.   

    $(this).attr("selected","selected");  
    改为
    jQuery(this).attr("selected",true );