我在使用下面的JQuery,JSP和Controller來返回在應用程序JSP頁面上更改國家下拉菜單時的城市列表,但我認爲jQuery中的dataType,成功參數不正確因爲我沒有收回清單。那麼有人可以告訴我我在做什麼錯了,以及如何通過更改國家/地區下拉菜單來獲得城市列表以將其添加到城市下拉菜單中?使用JQuery返回一個列表
感謝您的時間
<script type="text/javascript">
function loadCity(){
var countryData="countryId="+$(country).val();
$.ajax({
type: "POST",
url: "LoadCities.htm",
data:countryData,
dataType: "javascript",
success: function(cities){
loadCities(eval(cities))
}
});
}
function loadCities(citiesLst){
clearCitiesDD();
for(var i=0;i<citiesLst.length;i++){
var city=citiesLst[i];
appendCity(city.id,city.value);
}
}
function clearCitiesDD(){
$('#cityDD').find('option').remove();
}
function appendCity(id,cityName){
$('#cityDD').append($("<option id='"+id+"'>" + cityName + "</option>"));
}
}
</script>
,並在我的應用程序控制器我有以下代碼:
@RequestMapping(value = "/LoadCities.htm", method = RequestMethod.GET)
public @ResponseBody
List<City> getCityByCountryID(HttpServletRequest request, HttpServletResponse response)
throws Exception {
List<City> list=
icitys.getCitiesByCountry(Long.parseLong(request.getParameter("countryID")));
return list;
}
,並在JSP文件我有以下的城市和國家下拉菜單:
<tr>
<td align="right">Country</td>
<td align="left">
<select id ="country" name="country" onchange="loadCity()">
<option value="0">--select--</option>
<c:forEach var="countrylst" items="${countriesList}">
<option value="${countrylst.id}">${countrylst.countryName}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td align="right">City</td>
<td align="left">
<select id="cityDD" name="cityDD">
<option value="0">--select--</option>
</select>
</td>
</tr>
您是否在控制檯中檢查了響應,以確保它的格式符合您的預期? –
@RoryMcCrossan檢查了firebug中的響應,但沒有得到任何東西:(這是否意味着我的代碼是正確的?! – MChan
不,這意味着您的JSP中的getCityByCountryID不返回任何內容 –