Here is what i have and here is the html...what am i doing wrong?
$("#request_showdate_1i option[value='2012']").attr("selected","selected");
<select name="request[showdate(1i)]" id="request_showdate_1i">
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008"&开发者_C百科gt;2008</option>
<option value="2009">2009</option>
<option value="2010" selected="selected">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
</select>
All you need to do is:
$('#request_showdate_1i').val('2012');
jQuery will do the rest :)
See the second example here: http://api.jquery.com/val/#val2
Your code is correct (though Darryl Hein's answer is easier to type), so there must be some other problem.
Where is your JS code?
Is it inside a doc ready? ==> `$(function() { ... });
Is jQuery included, etc.
Here is a fully working HTML page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title> - jsFiddle demo</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.4.2.js'></script>
<link rel="stylesheet" type="text/css" href="/css/normalize.css"/>
<link rel="stylesheet" type="text/css" href="/css/result-light.css"/>
<style type='text/css'>
body {
background-color: transparent;
padding: 10px;
}
</style>
<script type='text/javascript'>
//<![CDATA[
$(function() {
$("#request_showdate_1i option[value='2012']").attr("selected","selected");
});
//]]>
</script>
</head>
<body>
<form>
<select name="request[showdate(1i)]" id="request_showdate_1i">
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010" selected="selected">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
</select>
<form>
</body>
</html>
精彩评论