1
I am developing a form in which I have a select that chooses states and capitals of brazil, but as those who fill can add more than one select I need to make a button add another select, is that in case I’m pulling this data from a json. Follow my code below
<div class="tab">Escreva nos espaços abaixo todas as cidades que compõe o seu Distrito:
<select id="estados" oninput="this.className = ''" name="estados" class="estados">
<option value=""></option>
</select>
<select id="cidades" oninput="this.className = ''" name="cidades" class="cidades">
</select>
<div class="adicionar"></div>
<p><button id="addCidade">Adicionar Cidade</button></p>
</div>
$(document).ready(function () {
$.getJSON('estados_cidades.json', function (data) {
var items = [];
var options = '<option value="">escolha um estado</option>';
$.each(data, function (key, val) {
options += '<option value="' + val.nome + '">' + val.nome + '</option>';
});
$("#estados").html(options);
$("#estados").change(function () {
var options_cidades = '';
var str = "";
$("#estados option:selected").each(function () {
str += $(this).text();
});
$.each(data, function (key, val) {
if(val.nome == str) {
$.each(val.cidades, function (key_city, val_city) {
options_cidades += '<option value="' + val_city + '">' + val_city + '</option>';
});
}
});
$("#cidades").html(options_cidades);
}).change();
});
});
But I want to take the values of my json, and with that I just cloned the field but without the values
– Thi100
So, the ideal was to add the new select only after the first one has been loaded, otherwise it makes no sense
– Sam