function buildSelect(str,sel) { sel.options.length=0; for(var i=0;i<str.length;i++) { sel.options[sel.options.length]=new Option(str[i],str[i]) } } function test(){ //alert("test"); } </script> <body> <select name="state" onChange="getResult(this.value)"> <option value="">請選擇</option>> <option value="zj">浙江</option>> <option value="zs">江蘇</option>> </select> <select id="city"></select> </body> </html> 二、servlet來源程式 package com.stephen.servlet; import java.io.IOException; import java.io.PrintWriter; import org.dom4j.*; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @author stephen * * TODO To change the template for this generated type comment go to Window - * Preferences - Java - Code Style - Code Templates */ public class SelectCityServlet extends HttpServlet { public SelectCityServlet() { super(); } public void destroy() { super.destroy(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF8"); response.setContentType("text/xml"); response.setHeader("Cache-Control", "no-cache"); String state = request.getParameter("state"); Document document = DocumentHelper.createDocument(); Element root = document.addElement("state"); Element city = null; if("zj".equals(state)){ city = root.addElement("city"); city.setText("杭州"); city = root.addElement("city"); city.setText("huzhou"); }else{ city = root.addElement("city"); city.setText("南京"); city = root.addElement("city"); city.setText("蘇州"); city = root.addElement("city"); city.setText("yangzhou"); } PrintWriter out=response.getWriter(); String s = root.asXML(); out.write(s); out.close(); } } |