2013-08-22 47 views
0

我在使用下面的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> 
+0

您是否在控制台中检查了响应,以确保它的格式符合您的预期? –

+0

@RoryMcCrossan检查了firebug中的响应,但没有得到任何东西:(这是否意味着我的代码是正确的?! – MChan

+0

不,这意味着您的JSP中的getCityByCountryID不返回任何内容 –

回答

2

希望你改变$(国家)$('#国)后得到解决。

还有一件事,你把dataType设置为“javascript”,但可用的数据类型是xml, json, script, or html

更好的尝试html

祝你好运。

+0

非常感谢,它使用html :) – MChan

0

应该

var countryData="countryId="+$(country).val(); 

var countryData="countryId="+$('#country').val(); 
+0

它已经是var countryData =“countryId =”+ $(country).val();我应该ge国家到#country? – MChan

+0

这就是我的建议。除非某些代码丢失,否则查询country元素的jQuery查询看起来不正确。 –

+0

我已经发布了我在这里的所有代码。仅供参考,它已经是没有#号的国家,如我在我的问题中粘贴的代码所示 – MChan