Answer the question
In order to leave comments, you need to log in
How to organize list of cities depending on country selection in select?
Hello. There is a form in which there are two selects, the first is the choice of the country, the second is the choice of the city. It is necessary that when choosing a certain country, only the cities of this country should be in the second select. I understand that it is necessary to organize using ajax, but there is not enough knowledge.
<select class="custom-select" name="country_from" id="country_from">
<option selected value=''>{% trans 'Russia' %}</option>
{% for item in country_list %}
<option value="{{item.id}}">{{item}}</option>
{% endfor %}
</select>
<div class="select_wrap">
<select name="city_from" id="id_city_from">
<option selected value=''>{% trans 'All' %}</option>
{% for item in city_list %}
<option value="{{item.id}}">{{item}}</option>
{% endfor %}
</div>
$(document).ready(function() {
$("#country_from").change(function(){
$.ajax({
type: 'GET',
url: '/city/search',
data: {country:$('#country_from option:selected').val()}
});
});
});
urlpatterns = [
path('search/', citysearch, name='city_search'),
]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question