5
I have a doubt, as I know nothing of PHP
, i would like someone to help me how to assemble or implement a Jquery
that I found.
In my case there are 2 selects, one who would be called Estado
and another Cidade
, and when selecting the Option in the Estado
, displays the others options (in the case the Cidades
) in the select Cidade
.
Until then I have it ready, I’ll leave the code for whoever needs it. What I would need is when to select the second city, display a Div with information in it.
For example:
- select: São Paulo
- select: São Bernardo
div: Lorem ipsum dolor sit Amet, consectetur adipiscing Elit. Duis varius nisi vitae neque malesuada vehicula at eget ante. Morbi a eros consectetur, lobortis justo ut, Elementum sapien. 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>
I see your code is all in javascript pure, you want an example of how to do this with javascript or jQuery?
– Erlon Charles
I think there’s something missing from the phrase "When selecting option displays div with". "With" what?
– Guilherme Nascimento