Here is what I have that properly populates the select list:
<select id="countriesList">
<option value="0"> -- select country -- </option>
<option value="500"> 500 </option>
</select>
<script type="text/javascript">
$(function () {
// load the countries list
$.getJSON(
"/StaticData/GetAllCountries/",
function (countries) {
// iterate each returned country and add it to the countries select list.
$.each(countries, function (i, country) {
$("#countriesList").append("<option value='" + country.Id + "'>" + country.Description + "</option>");
});
});
// set the selected country based on the model's delivered CountryId value
$('#countriesList').val(500);
});
</script>
The population works great, but calling .val() on one of the option elements added by the script does not make it selected, but as an example, the statically defined option "500" works fine when I call .val() on it.
I'm fairly new to jquery, but not programming, as I make my way into browser programming. Any tips would be useful. What else do you need?
This script code is sitting directly inside a table td tag so the select is populated and set as the form is rendered.