jsp里用Ajax做的select的两级级联
来源:互联网 发布:node fetch 编辑:程序博客网 时间:2024/05/16 17:29
jsp里用Ajax做的select的两级级联,带数据库。
jsp里的两个select:<select name="lei" onchange="callServer(this.options[this.selectedIndex].text)">
<%
rs=stmt.executeQuery("select dfl from dhyfl");
while(rs.next()){
%>
<option><%=rs.getString("dfl") %></option>
<%
}
%>
</select><select name="smallLei"></select>
<%
rs=stmt.executeQuery("select dfl from dhyfl");
while(rs.next()){
%>
<option><%=rs.getString("dfl") %></option>
<%
}
%>
</select><select name="smallLei"></select>
jsp底部的Ajax:
<script language="javascript">
var xmlHttp=false;
try {
xmlHttp = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
}
}
}
function callServer(smallLei){
var url="<%=request.getContextPath()%>/smallLei?lei=" + smallLei;
xmlHttp.open("get",url,"true");
xmlHttp.onreadystatechange =upsmlei;
xmlHttp.send(null);
}
function upsmlei(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status==200){
var smlei = document.all.smallLei;
smlei.options.length=0;
var arr=xmlHttp.responseText.split("|");
for(var i=0;i<arr.length-1;i++){
smlei.add(new Option(arr[i],arr[i]));
}
}
}
}
</script>
var xmlHttp=false;
try {
xmlHttp = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
}
}
}
function callServer(smallLei){
var url="<%=request.getContextPath()%>/smallLei?lei=" + smallLei;
xmlHttp.open("get",url,"true");
xmlHttp.onreadystatechange =upsmlei;
xmlHttp.send(null);
}
function upsmlei(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status==200){
var smlei = document.all.smallLei;
smlei.options.length=0;
var arr=xmlHttp.responseText.split("|");
for(var i=0;i<arr.length-1;i++){
smlei.add(new Option(arr[i],arr[i]));
}
}
}
}
</script>
Ajax的servlet:
package cqrx.ajax;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cqrx.zgdsc.Conn;
public class SmallLei extends HttpServlet {
public SmallLei() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
String lei = request.getParameter("lei");
lei = new String(lei.getBytes("ISO8859-1"), "GB2312");
out.print(lei);
Conn c = new Conn();
Statement stmt = null;
ResultSet rs = null;
try {
stmt = c.getConn().createStatement();
String sql = "select sfl from shyfl where dfl ='" + lei + "'";
rs = stmt.executeQuery(sql);
String str = "";
while (rs.next()) {
str = str + rs.getString(1) + "|";
}
out.print(str);
} catch (Exception e) {
System.out.print(e.getMessage());
} finally {
out.flush();
out.close();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void init() throws ServletException {
}
}
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cqrx.zgdsc.Conn;
public class SmallLei extends HttpServlet {
public SmallLei() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
String lei = request.getParameter("lei");
lei = new String(lei.getBytes("ISO8859-1"), "GB2312");
out.print(lei);
Conn c = new Conn();
Statement stmt = null;
ResultSet rs = null;
try {
stmt = c.getConn().createStatement();
String sql = "select sfl from shyfl where dfl ='" + lei + "'";
rs = stmt.executeQuery(sql);
String str = "";
while (rs.next()) {
str = str + rs.getString(1) + "|";
}
out.print(str);
} catch (Exception e) {
System.out.print(e.getMessage());
} finally {
out.flush();
out.close();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void init() throws ServletException {
}
}
转自:http://www.blogjava.net/lengxinCEO/archive/2007/07/31/133596.html
0 0
- jsp里用Ajax做的select的两级级联
- ajax做的二级级联
- 在JQUERYUI的Dialog对话框中用Ajax Extensions实际两级分类的DorpDownList无刷新级联
- 用jquery ajax做的select菜单,选中的效果
- 简单的两级联动ajax加载省市
- JQuery 中利用$.ajax()方法做的四级级联
- JSP+AJAX三级级联及更多级的实现。
- 用 jsp 写的 ajax 技术实现省市县等 n 级联动
- 关于SELECT的无限级联
- 关于select的无限级联
- 基于JQuery的Select级联
- select下拉列表的级联
- 用AJAX做下拉框级联
- 用DB+JSP+AJAX实现级联菜单
- servlet里获取jsp页面select的option值
- 两级联动菜单的实现之二 --- 用ajax实现菜单联动
- jsp jquery实现级联菜单,jquery对select元素的简单操作
- AJAX实例的级联菜单
- poj Common Subsequence 最长公共子序列
- 居中显示的Toast
- ofbiz 权限设计及动态二级菜单解决方案
- CodeForces 19A World Football Cup (简单题)
- [Flight] Pitch Yaw Roll的区别
- jsp里用Ajax做的select的两级级联
- mysql 时间函数总结
- 好用的网站
- DIV+CSS的命名规则
- 关于android安卓的静态数据库文件的存取问题
- 基于can总线节点ECU的设计
- The interface EntityDao cannot be implemented more than once with different arguments: EntityDao<Sit
- div与ul间出现不明空白 与css hack 区分ie7、ie8、ie9 以及firefox等其他浏览器
- Makefile 中:= ?= += =的区别