-2
Someone can help me with this logic?
Next I am filling a select Manufacturer with data from a json using Jquery, the problem is that I can not think of a way to fill the other select Model according to which I select in the manufacturer, I am new in this business of Jquery.
Meu Json
0
id 1
nome "Ford"
modelo
0
id 1
nome "mustang"
1
id 2
nome "fiesta"
2
id 3
nome "Ka"
1
id 2
nome "Fiat"
modelo
0
id 4
nome "Uno"
1
id 5
nome "Cronos"
2
id 6
nome "Argo"
2
id 3
nome "chevrolet"
modelo
0
id 7
nome "Camaro"
1
id 8
nome "Cruze"
2
id 9
nome "Onix"
Here this code of my form, Obviously the logic q developed below does not work, it just fills the select Manufacturer but n works in the precise model of a north of how to proceed.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
Fabricante:
<select name="fabricante" id="fabricante">
<option>Selecione um fabricante</option>
</select>
<br />
<br /> Modelo:
<select name="modelo" id="modelo">
</select>
<script src="<c:url value="js/jquery.js"/>"></script>
<script>
$(document)
.ready(
function() {
carregar_json('fabricante')
function carregar_json(id, id_modelo) {
var html = ""
$
.getJSON(
"http://localhost:8080/alucar/cadastroVeiculoJ",
function(data) {
html += "<option>Selecione "
+ id + "</option>";
console.log(data);
if (id == "fabricante"
&& id_modelo == null) {
for (var i = 0; i < data.length; i++) {
console
.log(data[i].nome)
html += "<option value="+data[i].id+">"
+ data[i].nome
+ "</option>";
}
} else {
for (var i = 0; i < data.lenght; i++) {
if (data[i].id == id_modelo) {
for (var j = 0; j < data[i].modelo.length; j++) {
html += "<option value="+data[i].modelo[j].id+">"
+ data[i].modelo[j].nome
+ "</option>"
}
}
}
}
$("#" + id).html(html);
})
}
$(document).on("change", '#fabricante', function() {
var id_modelo = $(this).val();
if (id_modelo != null) {
carregar_json("modelo", id_modelo);
}
})
})
</script>
</body>
</html>
Oops, thank you so much for the tip, it worked right, vlw even !!!!!!!
– Rogerinho do Inga