今天课上老师讲了一个联动下拉框的demo,
用的是jquery,没连接数据库,是用的JSON数组
研究了一晚上,貌似搞懂了,现在传上源码与jquery入门新手分享
servlet类
package com.newtest;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.newtest.vo.Province;
public class ProvinceAction extends HttpServlet {
/**
* Constructor of the object.
*/
public ProvinceAction() {
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 {
String oper=request.getParameter("oper");
if(oper==null){
List<Province> list=new ArrayList<Province>();
list.add(new Province(1,"四川"));
list.add(new Province(2,"广东"));
list.add(new Province(3, "湖南"));
request.setAttribute("provinceList", list);
request.getRequestDispatcher("city_pro.jsp").forward(request, response);
}else{
response.setContentType("text/html;charset=gbk");
String id=request.getParameter("provinceId");
System.out.println(id);
if("1".equals(id)){
JSONObject json=new JSONObject();
json.put("id",1);
json.put("name","成都");
JSONObject json2=new JSONObject();
json2.put("id",2);
json2.put("name","遂宁");
JSONObject json3=new JSONObject();
json3.put("id",3);
json3.put("name","乐山");
JSONArray ja = new JSONArray();
ja.add(json);
ja.add(json2);
ja.add(json3);
response.getWriter().println(ja.toString());
}
if("2".equals(id)){
JSONObject json=new JSONObject();
json.put("id",1);
json.put("name","广州");
JSONObject json2=new JSONObject();
json2.put("id",2);
json2.put("name","顺德");
JSONObject json3=new JSONObject();
json3.put("id",3);
json3.put("name","佛山");
JSONArray ja = new JSONArray();
ja.add(json);
ja.add(json2);
ja.add(json3);
response.getWriter().println(ja.toString());
}
if("3".equals(id)){
JSONObject json=new JSONObject();
json.put("id",1);
json.put("name","长沙");
JSONObject json2=new JSONObject();
json2.put("id",2);
json2.put("name","湘潭");
JSONObject json3=new JSONObject();
json3.put("id",3);
json3.put("name","娄底");
JSONArray ja = new JSONArray();
ja.add(json);
ja.add(json2);
ja.add(json3);
response.getWriter().println(ja.toString());
}
}
}
/**
* 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 {
doGet(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
实体类
package com.newtest.vo;
public class Province {
private int id;
private String name;
public Province(int id, String name) {
this.id=id;
this.name=name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'MyJsp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" src="script/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
function getCityById(obj){
var id = obj.value;
$.ajax({
url: '<%=path %>/Province',
type:'post',
data:{
provinceId:id,
oper:'get'
},
success:function(data){
var msg = eval('(' + data + ')');
var city = $("#city");
city[0].options.length = 1;
for(var i in msg){
var option = new Option(msg[i]['name'],msg[i]['id']);
city[0].add(option);
}
}
});
}
</script>
</head>
<body>
<center>
<h1>联动下拉框</h1>
<select id="province" onchange="getCityById(this);">
<option value="">--请选择省份--</option>
<c:forEach var="province" items="${requestScope.provinceList}">
<option value="${province.id}">${province.name }</option>
</c:forEach>
</select>
<select id="city">
<option value="">--请选择城市--</option>
</select>
</center>
</body>
</html>
是从servlet类进入,如:http://localhost:8080/test_jquery/Province (/Province是servlet配置的url-pattern)
分享到:
相关推荐
ajax 实现 (全国,省,是,区....) N级联 Demo js: <script type="text/javascript"> $(function () { $.fn.CascadeSelect({ url: '/CascadeSelect/CascadeSelect.ashx', //返回Json数据的一般处理文件 idKey: ...
winform 页面 下拉框 省市联动选择 demo 小功能组件 选择省后,市选项就自动变为该省内的选项
这里面的东西,下载后直接解压,双击demo.html就可以直接运行了。
这是一套VUE全家桶的Demo里面有数据来源,和JS代码,4级联动下拉组件,可以随意更改,可二次开发无限极联动。
用JQuery和select 做的一个 二级 下拉 联动 思路 就是 点击 第一个 下拉框时 调取 change事件 获取 第二个下拉框的值 实现 联动 效果
这是安卓spinner多级联动demo,实现省市区多级联动下拉框效果
html 下拉列表 联动选择 展示数据Demo 类似可以实现地址选择
多级联动,支持Ajax动态获取数据并缓存数据,动态生成下级select菜单,设置动态生成"option"第一个条目作为提示项,自动选择唯一选项的菜单,设置回调函数在onChange事件触发时获取下拉菜单相关信息。 详细功能及...
NULL 博文链接:https://xiaoyi3317.iteye.com/blog/732072
jquery实现三级联动的下拉框demo,是一个war包,可以在tomcat中直接运行,也可以自己解压了查看源代码
公司做项目的时候,需要用到下拉框联动显示数据的功能,索性利用Ajax来实现,看到时间比较充裕,就没去找demo自己去想方法写了。纯自己的想法,有些可能比较弱智,希望不要见笑。 页面中的两个下拉列表框: ...
基于layui框架的前端组件源码合集例如省市县三级联动下拉框、多选控件、骤条step、表格控件、穿梭框等,项目中直接导入,复制demo源码就能使用,开发方便,样式美观
qt天气预报demo,内容主要有 通过API来获取天气信息,在解析数据,显示到部件上,以及二级联动下拉框的使用,通过IP来定位,实现默认位置。局域网IP暂时不能获取到内网IP
SELECT_jQUERY下拉框...支持基本用法、宽度设置、下拉框分组、下拉框可编辑、下拉框联动、自定义列数、动态创建(修改)下拉框、恢复系统默认等功能。 本人在项目开发中的必备组件。 使用方法请详细阅读demo.html内容
原生JS写的多级联动下拉列表,联动级数可以自定义,不限级数。可用作省、市、区联动,或其他的多级分类选择。使用方法请参考demo文件。
Ajax+PHP二级联动下拉列表(实现下拉联动)
英文,纯js, 不用连数据 库,国家地区级联菜单 三级联动世界城市英文版 地区三级联动demo 三级联动世界城市英文版 地区三级联动demo
包含全国各地所有省市县的名称,三层下拉框联动选择,内附demo以及快速配置方法....
多选下拉列表 复选下拉列表 jquery多选列表 多选多级联动下拉列表,有demo
这里演示下拉菜单和文本框构建的介绍栏,将Select框中的值定位到INPUT文本输入框中,是下拉框赋值到文本框的实例,上网时候貌似经常见的功能,只是不知如何形容,或许叫做联动吧。 运行效果如下图所示: 在线演示...