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的问题,因为我把其中一个遍历删除了,运行出来,还是两个显示的相同的,这个应该怎么处理呢?
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的问题,因为我把其中一个遍历删除了,运行出来,还是两个显示的相同的,这个应该怎么处理呢?
<!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>
改为
jQuery(this).attr("selected",true );