Pessoal, estou utilizando AJAX e tenho uma dúvida que não encontrei exemplo que faça isso na web;
o que desejo fazer é “matar” o frame escondido que preenche um combo dependente de outro(por exemplo, combo de uf e combo de cidades).
já estou conseguindo trazer corretamente a string com os valores que devem ser colocados na outra combo, a string que retorno é da seguinte forma?
e etc…
com o frame esdondido, era feito de uma forma diferente da possível com AJAX…
Pois bem, minha aplicação retorna esta string para meu javascript, agora desejo coloca isso dentro do meu combo:
<select name=“cidades”> </select>
e se você colocar um DIV e o seu select dentro. Ao invés de atualizar somente as opções atualiza o select inteiro. O responseText conterá a sua tag select mais os options.
[code]
function processaMontaTrecho() {
if (req.readyState == 4) {
if (req.status == 200) {
div = window.opener.document.getElementById(campoRetorno);
div.innerHTML = "";
div.innerHTML = req.responseText;
window.close();
} else {
alert("Ocorreu o seguinte erro recuperando os trechos:\n" + req.statusText);
}
}
[quote=debersom]e se você colocar um DIV e o seu select dentro. Ao invés de atualizar somente as opções atualiza o select inteiro.
[/quote]
Boa sugestão… Pena que não posso utilizar este tipo de solução na aplicação…
package br.com.juliano;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Teste extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Constructor of the object.
*/
public Teste() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
String estado = request.getParameter("Estado");
StringBuffer retorno = new StringBuffer("");
PrintWriter out = response.getWriter();
out.println("<select name=\"Cidades\">");
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("string conexão jdbc","conta","senha");
String sql = "select "
+ " cid.idt_cidade, "
+ " cid.nom_cidade "
+ " from "
+ " pais pais, "
+ " estado est, "
+ " cidade cid "
+ " where "
+ " cid.sig_pais=pais.sig_pais "
+ " and cid.sig_estado=est.sig_estado (+) "
+ " and cid.sig_pais=est.sig_pais (+) "
+ " and cid.sig_estado = '" + estado + "'"
+ " order by cid.nom_cidade ";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
retorno.append("<option value=\""+rs.getString("IDT_CIDADE")+"\">"+rs.getString("NOM_CIDADE")+"</option>\n");
}
out.println(retorno);
if (conn != null) {
conn.close();
}
}
catch (Exception ex) {
throw new ServletException("Erro: "+ex);
}
out.println("</select>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
A lógica de funcionamento está nos arquivos acima, espero ter ajudado.