【发布时间】:2014-08-07 22:57:54
【问题描述】:
我有一个页面,允许用户从下拉列表中选择州、城市和部门。每个下拉列表都是根据前一个下拉列表的选定值填充的。当用户做出选择时,我将值存储在 cookie 中。然后,我在页面加载/重新加载时检查 cookie。页面重新加载,值看起来不错;但是,我无法在下拉框中设置值。我尝试对值进行硬编码,但它仍然不起作用。我错过了什么吗?
$(function () {
var checkState = checkCookie("SelectedState"); // get the state index that was saved in the cookie.
var cityDropdown = $('#CityName');
if (checkState != "" && checkState != null) {
var checkCityCookie = checkCookie("cityName"); // get the city index value
var newCity = checkCityCookie.replace("=", " ")
if (newCity == "") {
getCity(checkState); //populate the city drop down
}
if (newCity != null && newCity != "") {
$('#CityName').val(1); //Hard code value here and still not working.
var checkDepartment = checkCookie("SelectedDepartment");
if (checkDepartment != null && checkDepartment != "") {
getDepartment(newCity);
}
}
}
})
2014 年 8 月 7 日更新:
//This is a sample of the html file that is generated,
so there is a value 1. $('#CityName').val(1);
<select name="CityName" id="CityName" onchange="changeCityName()"><option value="">Choose School</option>
<option value="1">Miami</option>
<option value="2">West Palm</option>
</select>
【问题讨论】:
-
基本调试时间。控制台中的任何错误?在函数的开头使用
console.log()在您的每个if..else语句和console.log('#CityName');中添加一些日志记录到您的代码中,以查看您是否真的针对一个对象。现在再次检查控制台,看看您预期的日志是否正在发生。也可以尝试$('#CityName').val('1');,因为该值是字符串而不是整数。
标签: javascript asp.net asp.net-mvc-3