I want to create a drop-down list with countries and the second drop-down list with cities, which depends on selected value in the first list. And the list of cities should
So I have solved my problem with AJAX request and jQuery append.
Change Map
to Map
AJAX request
function sendAjaxRequest() {
var country = $("#country").val();
$.get( "/regions?country=" + country, function( data ) {
$("#region").empty();
data.forEach(function(item, i) {
var option = "";
$("#region").append(option);
});
});
};
Use sendAjaxRequest()
when i change first drop-down list.
$(document).ready(function() {
$("#country").change(function() {
sendAjaxRequest();
});
});
Drop-down list at the Thymeleaf template
First drop-down list
Second drop-down list
Controller
@RequestMapping(value = "/regions")
@ResponseBody
public Set getRegions(@RequestParam String country) {
Map> regions = regionsService.getRegions();
return regions.get(country);
}