I have a question, since I do not handle anything with PHP
, I would like someone to help me with how to mount or implement a Jquery
that I found.
In my case there are 2 selects , one that would be dubbed Estado
and another Cidade
, and when you select Option in Estado
, other options (in the case of Cidades
) in select Cidade
.
Until then I have it ready, I'll leave the code for whoever needs it. What I would need was when I select the second city, display a Div with information in it.
For example:
div: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis varius nisi vitae neque malesuada vehicula at eget ante. Morbi to eros consectetur, lobortis just ut, sapien elementum. Praesent et mollis ante. Vestibulum dignissim eros sed est imperdiet, at aliquam nunc egestas. Vestibulum ut diam eu erat condimentum lacinia pulvinar at turpis. Pellentesque ac odio ut ante interdum molestie.
var listadeCidades = new Array(4)
listadeCidades["Vazio"] = ["Cidade"];
listadeCidades["São Paulo"] = ["1a", "2a", "3a"];
listadeCidades["Rio de Janeiro"] = ["1b", "2b", "3b", "4b"];
listadeCidades["Paraná"] = ["1c", "2c", "3c"];
listadeCidades["Bahia"]= ["1d", "2d", "3d", "4d"];
function trocadeEstado(selectObj) {
var idx = selectObj.selectedIndex;
var which = selectObj.options[idx].value;
cList = listadeCidades[which];
var cSelect = document.getElementById("cidade_campo");
var len=cSelect.options.length;
while (cSelect.options.length > 0) {
cSelect.remove(0);
}
var newOption;
for (var i=0; i<cList.length; i++) {
newOption = document.createElement("option");
newOption.value = cList[i];
newOption.text=cList[i];
try {
cSelect.add(newOption);
}
catch (e) {
cSelect.appendChild(newOption);
}
}
}
<form action="" method="post">
<span><label for="estado"><strong>*</strong>Estado:</label>
<select name="estado_campo" id="estado_campo" onchange="trocadeEstado(this);" required>
<option value="Vazio">Estado</option>
<option value="São Paulo">São Paulo</option>
<option value="Rio de Janeiro">Rio de Janeiro</option>
<option value="Paraná">Paraná</option>
<option value="Bahia">Bahia</option>
</select>
</span>
<span>
<label for="cidade_campo"><strong>*</strong>Cidade:</label>
<select name="cidade_campo" id="cidade_campo" required>
<option value="0">Cidade</option>
</select>
</span>
</form>