There is a jsp-page, I pass on a list of countries to select, each country has a list of cities, which is in my database as a separate table. How to make that when choosing and changing the country - the city’s position has been updated and not selected?

Is the correct solution to load the list of countries from the database or is it ok to hard-code all 300 countries in html code?

I do not know js, but I think that if without it in any way, I will deal with the code. Thanks for the help.

  • without js it will not work. if you don’t do an end-point on the server side, you’ll have to add all countries to the page - Mikhail Vaysman
  • And with js can you tell me how this is done? I understand that I need to hang up the onchange listener to change the first select, and make an asynchronous request when changing, but I don’t have a clue how to do this (I’m talking about an asynchronous request). - uncleSAM
  • and how to write request processing on server side know? - Mikhail Vaysman
  • I wrote the usual requests (which are made by clicking on the submit button), if this one is no different - then yes. - uncleSAM

1 answer 1

Here's a jQuery option.

$("#countries").change(function () { $.getJSON("/cities", null, function (data) { $("#cities option").remove(); $.each(data.cities, function (index, item) { $("#cities").append( $("<option></option>").text(item.name).val(item.id) ) }); }); }); 
  • At first, when I looked, it seemed to me abrai kadabra, but then, then, however, I vrodeby saw the logic in this) thanks, I will try with your code. Only I will have to return not JSON, but Javovsky ArrayList. Does this change the situation? - uncleSAM
  • will change - everything will stop working. set up the correct conversion of objects. mostly web uses json. if the answer is right, accept it. - Mikhail Vaysman
  • Ok, I'll figure it out (just a little experience so far). Thank! - uncleSAM
  • what framework do you use? What to fix in the answer? - Mikhail Vaysman
  • I'm far away on bare servlets. nothing needs to be fixed. thank! - uncleSAM