How to dynamically load combo boxes with Ajax (code attached)

php中世界最好的语言
Release: 2018-03-31 10:16:24
Original
1242 people have browsed it

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"%>    动态加载组合框 
  
省份:
城市:
Copy after login

二、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"+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(""); PrintWriter out = response.getWriter(); out.println(city.toString()); out.flush(); //输出流刷新 out.close(); //关闭输出流 } /* * 初始化城市 */ public List 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); } }
Copy after login

三web.xml

   CityByXMLServlet servlet.CityByXMLServlet   CityByXMLServlet /CityByXMLServlet 
Copy after login
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!