Pessoal peguei este exemplo na internet e estou tentando adaptar para atender a minha necessiadade, porem estou tendo o seguinte problemas:
Tenho um combo e quando selecione um item dele, via jquery o e feita uma consulta no banco e me retorna o valor, para ser carregado no segundo combo.
Tenho o seguinte js na pagina que tem o combo.
function buscaFilho(cod_pai){
try{
xmlhttp = new XMLHttpRequest();
} catch(e1) {
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e2){
try{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e3){
xmlhttp = false;
}
}
}
//limpa a combobox
var c = document.getElementById("txtPasta1")
while( c.options.length > 0) c.options[0] = null
c.options[0] = new Option("... Wait a moment ... ","... Wait a moment ...")
//Monta a url com nome da pasta pai
xmlhttp.open("GET", "ajax.jsp?nom_pasta=" + cod_pai, true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4){
var c = document.getElementById("txtPasta1")
while(c.options.length > 0) c.options[0] = null
//Transforma a lista de Pastas JSON em Javascript
var aPastas = eval((xmlhttp.responseText))
//popula o select com a lista de Pastas obtida
for(var i = 0; i < aPastas.length; i++){
aPastas[i] = unescape(aPastas[i])
c.options[c.options.length] = new Option(aPastas[i], aPastas[i])
}
}
}
xmlhttp.send(null)
}
que executa o jsp, ajax.jsp
<%@page import="banco.operacoes"%>
<%@page import="funcoes.especializados"%>
<%@page import="funcoes.funcionalidades"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="java.util.List"%>
<%@ page language="java"%>
<%@ page contentType="text/html"%>
<%@ page session="true"%>
<html>
<head>
<link rel="shortcut icon" href="resources/if.ico">
<title>Cemicres</title>
<meta http-equiv=content-type charset=ISO-8859-1 />
<%
String vEmpresa = (String) session.getAttribute("SessionEmpresa");
%>
<script type="text/javascript">
function buscaFilho(cod_pai){
try{
alert(cod_pai)
xmlhttp = new XMLHttpRequest();
} catch(e1) {
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e2){
try{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e3){
xmlhttp = false;
}
}
}
}
</script>
<style type=text/css>
@import url(styles.css);
<%
String vCodPai = null;
try{
funcionalidades func = new funcionalidades();
vCodPai = request.getParameter("nom_pasta").toString();
List lista = func.listaFolders(vCodPai,vEmpresa);
out.print("[");
for(int i = 0; i < lista.size(); i++){
if (i != 0) out.print(", ");
out.print("'" + lista.get(i).toString() + "'");
System.out.println(lista.get(i).toString());
}
out.print("]");
} catch (NumberFormatException e){
e.printStackTrace();
} catch (NullPointerException e){
System.out.println(vCodPai);
e.printStackTrace();
}
%>
</style>
</head>
<body>
</body>
</html>
Verifiquei que o retorno está fazendo certo, porem não está carregando no combo.
o que pode estar errado ???
Outra coisa, depois vou precisar estender isso a outros combos, alguem saberia como fazer ???