功能描述
任意选择两个下拉框,根据选的值作为条件从服务器取得满足条件的值,显示在新的类型下拉框里。
文件一览
- dynamicLists.html
- RefreshModelListServlet.java
dynamicLists.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Dynamically Filling Lists</title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
function refreshModeList() {
var make = document.getElementById("make").value;
var modelYear = document.getElementById("modelYear").value;
if (make == "" || modelYear == "") {
clearModelsList();
return;
}
var url = "RefreshModelListServlet?"
+createQueryString(make,modelYear) + "&ts=" + new Date().getTime();
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET",url, true);
xmlHttp.send(null);
}
function createQueryString(make,modelYear) {
var queryString = "make=" + make + "&modelYear=" + modelYear;
return queryString;
}
function handleStateChange() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
updateModelList();
}
}
}
function updateModelList() {
clearModelsList();
var models = document.getElementById("models");
var results = xmlHttp.responseXML.getElementsByTagName("model");
var option = null;
for (var i=0; i<results.length;i++) {
option = document.createElement("option");
option.appendChild(document.createTextNode(results[i].firstChild.nodeValue));
models.appendChild(option);
}
}
function clearModelsList() {
var models = document.getElementById("models");
while(models.childNodes.length > 0) {
models.removeChild(models.childNodes[0]);
}
}
</script>
</head>
<body>
<h1>Select Model Year and Make</h1>
<form action="#">
<span style="font-weight:bold;">Model Year:</span>
<select id="modelYear" onchange="refreshModeList()">
<option value="">Select One</option>
<option value="2006">2006</option>
<option value="1995">1995</option>
<option value="1985">1985</option>
<option value="1970">1970</option>
</select>
<br />
<br />
<span style="font-weight:bold;">Make:</span>
<select id="make" onchange="refreshModeList()">
<option value="">Select One</option>
<option value="Chevrolet">Chevrolet</option>
<option value="Dodge">Dodge</option>
<option value="Pontiac">Pontiac</option>
</select>
<br />
<br />
<span style="font-weight:bold;">Models:</span>
<br />
<select id="models" size="6" style="width:300px">
</select>
</form>
</body>
</html>
RefreshModelListServlet.java
package ajaxbook.chap4;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RefreshModelListServlet extends HttpServlet {
private static List availableModels = new ArrayList();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
processRequest(req, resp);
}
@Override
public void init() throws ServletException {
availableModels.add(new MakeModeYear(2006,"Dodge","Charger"));
availableModels.add(new MakeModeYear(2006,"Dodge","Magnum"));
availableModels.add(new MakeModeYear(2006,"Dodge","Ram"));
availableModels.add(new MakeModeYear(2006,"Dodge","Viper"));
availableModels.add(new MakeModeYear(1995,"Dodge","Avenger"));
availableModels.add(new MakeModeYear(1995,"Dodge","Intrepid"));
availableModels.add(new MakeModeYear(1995,"Dodge","Neon"));
availableModels.add(new MakeModeYear(1995,"Dodge","Spirit"));
availableModels.add(new MakeModeYear(1985,"Dodge","Aries"));
availableModels.add(new MakeModeYear(1985,"Dodge","Daytona"));
availableModels.add(new MakeModeYear(1985,"Dodge","Omni"));
availableModels.add(new MakeModeYear(1970,"Dodge","Challenger"));
availableModels.add(new MakeModeYear(1970,"Dodge","Charger"));
availableModels.add(new MakeModeYear(1970,"Dodge","Coronet"));
availableModels.add(new MakeModeYear(1970,"Dodge","Dart"));
availableModels.add(new MakeModeYear(2006,"Chevrolet","Colorado"));
availableModels.add(new MakeModeYear(2006,"Chevrolet","Corvette"));
availableModels.add(new MakeModeYear(2006,"Chevrolet","Equinox"));
availableModels.add(new MakeModeYear(2006,"Chevrolet","Monte Carlo"));
availableModels.add(new MakeModeYear(1995,"Chevrolet","Beretta"));
availableModels.add(new MakeModeYear(1995,"Chevrolet","Camaro"));
availableModels.add(new MakeModeYear(1995,"Chevrolet","Cavalier"));
availableModels.add(new MakeModeYear(1995,"Chevrolet","Lumina"));
availableModels.add(new MakeModeYear(1985,"Chevrolet","Chevette"));
availableModels.add(new MakeModeYear(1985,"Chevrolet","Celebrity"));
availableModels.add(new MakeModeYear(1985,"Chevrolet","Citation"));
availableModels.add(new MakeModeYear(1985,"Chevrolet","Charger"));
availableModels.add(new MakeModeYear(1970,"Chevrolet","Bel Air"));
availableModels.add(new MakeModeYear(1970,"Chevrolet","Caprice"));
availableModels.add(new MakeModeYear(1970,"Chevrolet","Chevelle"));
availableModels.add(new MakeModeYear(1970,"Chevrolet","Monte Carlo"));
availableModels.add(new MakeModeYear(2006,"Pontiac","G6"));
availableModels.add(new MakeModeYear(2006,"Pontiac","Grand Prix"));
availableModels.add(new MakeModeYear(2006,"Pontiac","Solstice"));
availableModels.add(new MakeModeYear(2006,"Pontiac","Vibe"));
availableModels.add(new MakeModeYear(1995,"Pontiac","Bonneville"));
availableModels.add(new MakeModeYear(1995,"Pontiac","Grand am"));
availableModels.add(new MakeModeYear(1995,"Pontiac","Grand Prix"));
availableModels.add(new MakeModeYear(1995,"Pontiac","Firebird"));
availableModels.add(new MakeModeYear(1985,"Pontiac","6000"));
availableModels.add(new MakeModeYear(1985,"Pontiac","Fiero"));
availableModels.add(new MakeModeYear(1985,"Pontiac","Grand Prix"));
availableModels.add(new MakeModeYear(1985,"Pontiac","Parisienne"));
availableModels.add(new MakeModeYear(1970,"Pontiac","Catalina"));
availableModels.add(new MakeModeYear(1970,"Pontiac","GTO"));
availableModels.add(new MakeModeYear(1970,"Pontiac","LEMans"));
availableModels.add(new MakeModeYear(1970,"Pontiac","Tempest"));
}
protected void processRequest(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException {
resp.setContentType("test/html;charset=UTF-8");
int modelYear = Integer.parseInt(req.getParameter("modelYear"));
String make = req.getParameter("make");
StringBuffer results = new StringBuffer("<models>");
MakeModeYear availableModel = null;
for (Iterator it = availableModels.iterator();it.hasNext();) {
availableModel = (MakeModeYear)it.next();
if (availableModel.modelYear == modelYear) {
if (availableModel.make.equals(make)) {
results.append("<model>");
results.append(availableModel.model);
results.append("</model>");
}
}
}
results.append("</models>");
//设置返回文件的类型
resp.setContentType("text/xml");
//向页面输出东西
resp.getWriter().write(results.toString());
}
private static class MakeModeYear {
private int modelYear;
private String make;
private String model;
public MakeModeYear(int modelYear, String make, String model) {
this.modelYear = modelYear;
this.make = make;
this.model = model;
}
}
}
效果如下图
- 大小: 20.8 KB
分享到:
相关推荐
下拉列表框的初始状态可能只包含一个“加载中”或“请选择”选项,其余选项在Ajax请求成功后动态添加。 7. **CSS样式** 为了使加载提示和错误信息更具视觉吸引力,可以使用CSS来定制样式。同时,也可以通过CSS优化...
在"ajaxcc(动态加载列表框)"这个项目中,我们关注的是如何利用Ajax来实现动态加载下拉列表框的内容。动态加载列表框是一种常见的交互设计,它能够在用户滚动或选择某个选项时,根据需求动态地从服务器获取并显示新的...
**Ajax 下拉列表框详解** Ajax(Asynchronous JavaScript and XML)技术是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术。在Web开发中,它极大地提升了用户体验,使得页面交互更加流畅。本教程将...
### 使用Eclipse与AJAX技术在JSP中实现动态加载列表框 #### 一、引言 随着Web技术的发展,用户对于网页交互性与实时性的需求越来越高。传统的Web应用程序通过表单提交的方式与服务器交互,这往往导致用户体验不佳...
在网页设计中,动态下拉列表框...总结来说,AJAX实现的动态下拉列表框利用了异步通信的能力,提升了用户体验,使网页更加互动和高效。开发者需要掌握JavaScript、DOM操作以及服务器端编程技术,才能成功实现这一功能。
在IT领域,动态加载下拉列表框是一种常见的交互设计,特别是在网页或应用程序中。它允许用户从一个可扩展的选项列表中进行选择,而这些选项可能不会一次性全部显示,而是根据用户的操作动态加载。这种技术提高了用户...
在本项目中,我们利用jQuery Masonry插件来实现瀑布流效果,并结合Ajax技术实现动态加载数据,进一步提升用户体验。 jQuery Masonry插件是由David DeSandro开发的一个JavaScript库,它的主要功能是将网页中的元素以...
本示例中的"jQuery ajax JSON 下拉列表框级联"就是这样的一个应用。 1. **jQuery**:jQuery是一个高效、简洁且功能丰富的JavaScript库,它简化了HTML文档遍历、事件处理、动画以及Ajax交互。在本案例中,jQuery用于...
本文将深入探讨如何使用Ajax实现下拉列表(Select)改变时动态创建单选按钮(Radio Button)组,同时也会涉及列表框、分组以及动态创建等关键知识点。 首先,我们需要理解Ajax的基本原理。Ajax允许我们在不刷新整个...
jquery 实现ajax动态加载下拉框搜索框内容。
JavaScript动态级联下拉列表框是一种常见的前端交互设计,它允许用户在选择一个选项后,根据选择的结果动态更新下一个下拉列表框的内容。这种技术在数据筛选、表单填写等场景中广泛应用,能够提高用户体验,减少不必...
级联下拉列表通常用于关联数据的筛选,例如省份-城市-区县的选择,当用户在一级下拉列表中选择一个选项时,二级下拉列表会动态加载与之相关的数据。 首先,我们需要理解Ajax的工作原理。Ajax允许我们通过JavaScript...
本示例——“Ajax示例中国省份、城市、城镇联动选择列表框”,就是运用Ajax实现的地理信息联动选择功能,适用于网页表单中,用户可以根据自己的需求逐步选择省份、城市及城镇,系统会根据前一层次的选择自动填充下一...
在网页设计中,Ajax(Asynchronous JavaScript and XML)技术被广泛用于实现动态交互效果,其中“Ajax 动态提示下拉列表”是一个常见的应用场景。这种功能允许用户在输入框中输入字符时,通过Ajax请求从服务器获取...
最后,将option元素添加到id为city的select元素中,从而实现了动态加载地市到下拉列表的操作。 页面结构和JavaScript代码整合: 文中的HTML页面部分包含一个省份和一个城市的选择框,用户选择省份后,城市选择框...
5. `JS`、`CSS` 和 `Inc` 目录:这些目录可能分别包含了JavaScript代码(用于处理用户交互和动态加载数据)、样式表(定义列表框的外观和布局)以及包含通用函数或组件的包含文件。 6. `Data` 目录:可能存储了省份...
"Ajax动态加载关键词提示"是Ajax应用的一个常见场景,它主要用于搜索框或者输入字段,当用户输入时,后台服务通过Ajax请求获取匹配的关键词,并实时显示给用户作为提示。这种功能在许多网站和应用中都有应用,例如...
总的来说,"Ajax动态树形列表"是前端开发中一种高效、交互性强的数据展示手段,它结合了Ajax的异步特性与树形结构的层次展示,为用户提供了便捷的操作体验。在实际项目中,开发者需要熟练掌握相关技术和工具,以实现...
6. **前端渲染**:前端接收到JSON响应后,解析数据,并将建议项动态添加到结果列表中。用户可以通过下拉菜单选择一个建议,或者继续输入。 7. **优化用户体验**:为了提升用户体验,可以设置延迟发送请求,比如用户...
总的来说,"Ajax城市 城镇联动下拉列表框"是通过AJAX技术和各种编程语言实现的一种动态交互功能。它提高了用户体验,减少了网络流量,同时也对后端数据处理和前端展示提出了挑战。理解并掌握这一技术,无论是对于...