This time I will show you how to dynamically load a combo box with Ajax (with code). What are theprecautions for Ajax to dynamically load a combo box? The following is a practical case, let's take a look.
一 province.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>动态加载组合框
省份: | |
城市: |
二、CityByXMLServlet.java
package servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.*; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class CityByXMLServlet */ @WebServlet("/CityByXMLServlet") public class CityByXMLServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static final String CONTENT_TYPE = "text/xml; charset=UTF-8"; /** * @see HttpServlet#HttpServlet() */ public CityByXMLServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); //设置服务器响应类型 String province =request.getParameter("province"); StringBuffer city = new StringBuffer(""); //记录返回XML串的对象 if("gx".equals(province)){ List list=cityInit(); //获取城市列表 for(int i=0;i "); PrintWriter out = response.getWriter(); out.println(city.toString()); out.flush(); //输出流刷新 out.close(); //关闭输出流 } /* * 初始化城市 */ public List"+list.get(i)+""); } }else if("hn".equals(province)){ List list = cityInit1(); //获取城市列表 for(int j=0;j "+list.get(j)+""); } }else if("hb".equals(province)){ List list = cityInit2(); //获取城市列表 for(int j=0;j "+list.get(j)+""); } } city.append(" cityInit2() { List cityList = new ArrayList (); //添加城市列表 cityList.add("武汉"); cityList.add("襄阳"); cityList.add("黄冈"); cityList.add("荆门"); cityList.add("十堰"); cityList.add("黄石"); return cityList; } public List cityInit(){ List cityList = new ArrayList (); //添加城市列表 cityList.add("南宁"); cityList.add("桂林"); cityList.add("北海"); cityList.add("河池"); cityList.add("梧州"); cityList.add("玉林"); return cityList; } public List cityInit1() { List cityList = new ArrayList (); //添加城市列表 cityList.add("长沙"); cityList.add("湘潭"); cityList.add("岳阳"); cityList.add("常德"); cityList.add("衡阳"); cityList.add("邵阳"); return cityList; } /** *当前Servelt的初始化方法.
* * @throws ServletException发生ServletExceptio时抛出 */ public void init() throws ServletException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
三web.xml
CityByXMLServlet servlet.CityByXMLServlet CityByXMLServlet /CityByXMLServlet
Asynchronous implementation of ajax file upload form
Asynchronous implementation of ajax file download
The above is the detailed content of How to dynamically load combo boxes with Ajax (code attached). For more information, please follow other related articles on the PHP Chinese website!