xmlhttp是一种方法,如果不想用的话,还可以在jsp里面直接把所有的省和市先查找出来,然后通过switch把它们一一对应起来,然后在循环生成select,下面是一个小例子。
解决方案 »
- jsp怎么读取绝对路径tomcat下临时文件夹temp下的图片?
- jsp+sql2000,怎么做上传下载?
- 散发贴
- 问一下tomcat项目乱码问题,css加载问题以及jsp插件
- 如何用jdbc连接SQL Server数据库
- 如何删除动态生成的TABLE?
- 小菜泣血跪求复选框问题
- 请问如何在struts的框架下 点button弹出一个新的窗口!急!
- 将一这段php代码用java怎么实现。高分相送。
- +++++++++++++++++++++++一个字闷!!!!++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- 数据表关联问题
- JSP/WAP开发达人入,显示内容的问题
这样主页面就不需要刷新也能实现关联
function changeCity(){
t = document.form1.selectProvince;//区局ID
n1 = document.form1.selectCity;//辖区ID
update_city(t.options[t.options.selectedIndex].value, n1);
return ;
}
function update_city(CatID1, stationF){
stationF.length=1;
switch ( CatID1 ){
<%
//遍历所有省的集合
ArrayList provinceCol= *; (provinceCol为所有省的集合)
ArrayList cityCol = *; (cityCol为所有市的集合) int count = 1;
if(provinceCol!=null&&provinceCol.size()>0){
Iterator it = provinceCol.iterator();
while(it.hasNext()){
ProvinceVO pvo = (ProvinceVO)it.next();
%>
case "<%=pvo.getProvinceId%>":
<%
//遍历所有市的集合
count = 1;
if (cityCol!=null&&cityCol.size()>0) {
Iterator it2 = cityCol.iterator();
while(it2.hasNext()){
CityVO cvo = (CityVO)it2.next();
if ((pvo.getProvinceId).equals(cvo.getProvinceId)) {
String cityName = cvo.getCityName();
String cityId = cvo.getCityId();
%>
stationF.options[<%=count++%>]=new Option("<%=cityName %>","<%=cityId %>");
<%
}
}
}
%>
break;
<%
}
}
%>
}
return -1;
}大致代码是这样的,可能还需要实际调试一下,如果有什么问题,欢迎提出讨论.
这种方法适用与关联不是很多的情况,如果是很多个select关联在一起,建议用xmlhttp来实现
<script language="JavaScript">
var city = new Array();
<%
int city=0;
Vector city_v = Common.getCityDict("BornPlaceCode");
Hashtable city_h = null;
Enumeration city_e = city_v.elements();
while(city_e.hasMoreElements()) {
city_h=(Hashtable)city_e.nextElement();
%>
city[<%=city%>]=new Array('<%=city_h.get("parent")%>','<%=city_h.get("name")%>','<%=city_h.get("code")%>');
<%
city+=1;
}
%>
function changeselect(BornPlaceCode){
document.EM2Form1.BornPlaceCode.length = 0;
document.EM2Form1.BornPlaceCode.options[0] = new Option('','');
for (i=0; i<city.length; i++)
{
if (city[i][0] == BornPlaceCode)
{
document.EM2Form1.BornPlaceCode.options[document.EM2Form1.BornPlaceCode.length] = new Option(city[i][1], city[i][2]);
if(city[i][2]=="<%=BornPlaceCode%>"){
document.EM2Form1.BornPlaceCode.options[document.EM2Form1.BornPlaceCode.length].selected=true;
}
}
}
}
{
var province_id = document.form1.Province_id.value
if(province_id == "")
return false var Str = new Array()
var selectStr <% while(province_rs.next()) { %>
selectStr = ' <% key.setSqlString("select * from province where Parent_id = "+province_rs.getInt("Id")); city_rs = key.executeQuery(); %>'
Str[<%=province_rs.getInt("Id")%>]= " <select name='City_id'><option value=''>------------------------</option><% while(city_rs.next()) { %><option value='<%=city_rs.getInt("Id")%>'><%=city_rs.getString("Name")%></option><% } %></select>"
<% city_rs.close(); } %>
if(province_id == 0)
window.city.innerHTML = "<select name='City_id'><option value=''>----------------------</option></select>"
else
window.city.innerHTML = Str[province_id]
return true
}
<script language="JavaScript">
var citycount;
var city = new Array();
<%
int i=0;
while(rs.next()){
key.setSqlString("select * from province where Parent_id = "+rs.getInt("Id"));
city_rs = key.executeQuery();
while(city_rs.next()){
%>
city[<%=i%>] = new Array("<%=city_rs.getInt("Id")%>","<%=city_rs.getString("Name")%>","<%=rs.getString("Id")%>"); //定义数组
<%
i++;
}//while(city_rs)
}//while(rs)
%>
count=<%=i%>
function showCity(Province_id){
document.hotel.City_id.length = 0;
var j;
document.hotel.City_id.options[0] = new Option('=======城市========','0');
document.hotel.City_id.options[0].selected=true;
for (j=0;j <count; j++){
if (city[j][2] == Province_id){
document.hotel.City_id.options[document.hotel.City_id.length] = new Option(city[j][1],city[j][0]);
}//if
} //for
if(document.hotel.Province_id.options[document.hotel.Province_id.selectedIndex].index>0){
document.hotel.City_id.options[1].selected=false;
}//if
}
</script>都运行过的!
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<script language="JavaScript">
function goSelCheJian(){
document.form3.action="selCheJian.htm"
document.form3.submit()
}
//搜索时,最大的时间默认为当前时间function showSel(){
alert("dfdf")
}</script>
<body>
<form name="form3" action="cheJian.htm" method="post">
<table border="0"><tr><td>
</td>
<td>
搜索:<select name="sel" onchange="showSel()">
<option value="selAll">所有</option>
<option value="selDate">日期</option>
<option value="selMember">采购员</option>
<option value="selMoney">价格</option>
<option value="selWare">商品名</option>
<option value="selType">类型</option>
</select>
</td>
<td><div id="myid"></div>
</td>
</tr>
</table>
<input type="button" value="dfdf" onclick="showSel()">
</form>
</body>
</html>
<!--保存为a.html 试一下,是根据id变换<div></div>的值---->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<script language="JavaScript">
function goSelCheJian(){
document.form3.action="selCheJian.jsp?curPage=1"
document.form3.submit()
}
//搜索时,最大的时间默认为当前时间function showSel(){
if(document.form3.sel.value=="selAll")
document.location="cheJian.jsp"
if(document.form3.sel.value=="selDate")
document.all.item('myid').outerHTML="<div id='myid'>起始日期:<input type=text name=selDate1 id=selDate1 size=20 ><img align=absMiddle alt=选择日期 height=16 id=daysOfMonthPos name=daysOfMonthPos onMouseUp=javascript:toggleDatePicker('selDate1Div','form3.selDate1') src=../public/clock.gif style=CURSOR: hand width=16><div id=selDate1Div style='POSITION: absolute'></div> 终止日期:<input type=text name=selDate2 id=selDate2 size=20 ><img align=absMiddle alt=选择日期 height=16 id=daysOfMonthPos name=daysOfMonthPos onMouseUp=javascript:toggleDatePicker('selDate2Div','form3.selDate2') src=../public/clock.gif style=CURSOR: hand width=16><div id=selDate2Div style='POSITION: absolute'></div> <input type=button value='搜索' onclick='goSelCheJian()'></div>"
if(document.form3.sel.value=="selMember")
document.all.item('myid').outerHTML="<div id='myid'>采购员:<input type='text' name='selMember' size=10 > <input type=button value='搜索' onclick='goSelCheJian()'></div>"
if(document.form3.sel.value=="selMoney")
document.all.item('myid').outerHTML="<div id='myid'>次购买不小于:<input type='text' name='selMoney1' size=20 >元 次购买不大于:<input type='text' name='selMoney2' size=20 >元 <input type=button value='搜索' onclick='goSelCheJian()'></div>"
if(document.form3.sel.value=="selWare")
document.all.item('myid').outerHTML="<div id='myid'>商品名:<input type='text' name='selWare' size=10 > <input type=button value='搜索' onclick='goSelCheJian()'></div>"
if(document.form3.sel.value=="selType")
document.all.item('myid').outerHTML="<div id='myid'>类型:<input type='text' name='selType' size=10 > <input type=button value='搜索' onclick='goSelCheJian()'></div>"
}</script>
<body></body>
</html>
{
//遍历所有适合的数据放入arrResult数组中.
arrResult[arrResult.length] = rs.getString("city");
rs.MoveNext();
}
//escape解决了XMLHTTP。中文处理的问题.
//数组组合成字符串.由","字符串连接.
Response.Write(escape(arrResult.join(",")));只要把上面的asp改成jsp即可。关键的就是join方法提示没有。整个ASP源码请参照:http://dev.csdn.net/article/28/28324.shtm希望能够有人帮助我把它改成jsp版本的,以免除这一级联问题的长期困扰。谢谢!
xmlhttp的用法如下:javascript 中的方法://onchange里调用的方法
function changeCity(){
var provinceId = document.form1.selectProvince.value; //获得所选的省的id
var url = ****/GetCity.jsp?provinceId ="+provinceId ; //把省id传到方法中,根据省id来取得城市的集合(我这里是直接做了一个jsp,在里面来查询,下面会把这个jsp给贴出来).
var xmlhttp = null; xmlhttp = new ActiveXObject(getXmlHttpPrefix()+".XMLHTTP");
xmlhttp.open("GET", url, false);
xmlhttp.send(); returnHtml=xmlhttp.ResponseText;
document.getElementById("cityDiv").innerHTML = returnHtml;
}
//此方法用来获得合适的xml插件
function getXmlHttpPrefix() {
if (getXmlHttpPrefix.prefix)
return getXmlHttpPrefix.prefix; var prefixes = ["Microsoft", "MSXML", "MSXML2", "MSXML3"];
var o;
for (var i = 0; i < prefixes.length; i++) {
try {
// try to create the objects
o = new ActiveXObject(prefixes[i] + ".XmlHttp");
return getXmlHttpPrefix.prefix = prefixes[i];
}
catch (ex) {};
} throw new Error("Could not find an installed XML parser");
}
<%
String CONTENT_TYPE = "text/xml;charset=GBK";
response.setContentType(CONTENT_TYPE); response.setHeader("Cache-Control","no-store"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0); //防止被proxy server cache //这里是我自己做的后台持久层,直接实例化就可以用了,你可以根据你自己的具体情况来改写
CityInfoJB cjb = new CityInfoJB ();
CityInfoVO cvo = new CityInfoVO (); Collection cityCol = new ArrayList(); String provinceId = request.getParameter("cscId");
String cityName = "";
String cityId = ""; StringBuffer strHtml = new StringBuffer(); if (provinceId != null && provinceId .length() > 0)
{
cityCol= cjb.getCityByProvinceId(provinceId ); if (cityCol!= null && cityCol.size() > 0)
{
Iterator it = cityCol.iterator(); if (it.hasNext())
{
strHtml.append(
"<select name='cityId' onchange='(此处可以做二级联动)'>");
while (it.hasNext())
{
cvo = (CityInfoVO) it.next();
strHtml.append("<option value='");
strHtml.append(cvo.getCity_id());
if ( (!"".equals(cityId))&&cvo.getCity_id().equalsIgnoreCase(cityId)) //如果cityId有初试值
strHtml.append("' selected>");
else
strHtml.append("'>");
strHtml.append(cvo.getCity_name());
strHtml.append("</option>");
}
strHtml.append("</select>");
}
}
else
{
strHtml.append("<select name='cityId'> ");
strHtml.append("<option value=''>");
strHtml.append("无对应城市");
strHtml.append("</option>");
strHtml.append("</select>");
}
}else{
strHtml.append("<select name='cityId'> ");
strHtml.append("<option value=''>");
strHtml.append("无对应城市");
strHtml.append("</option>");
strHtml.append("</select>");
}
out.println(strHtml.toString());%>
我说的已经够清楚了呢,你要是再搞不定,干脆提供几个接口给我调用,我帮你把页面给写出来,你直接在那个被调用的jsp里面,import进那几个方法,就行了。
http://community.csdn.net/Expert/topic/3765/3765064.xml?temp=.8633539
报个到,那里也有一点分^_^
CityInfoVO cvo = new CityInfoVO ();这两个方法应该给我class文件吧?还有
String provinceId = request.getParameter("cscId");“cscId”在那里声明的?不好意思,麻烦给解释一下。
能具体说一下 cityinfojb和cityinfovo的用途吗?他们的作用应该不一样的。
如果你要是觉得麻烦,就直接写一个java方法,通过provinceid查出对应的城市的resultset,然后用rs来做循环就可以了,只是那样做的看起来有点别扭罢了:)
http://www.csdn.com.cn/program/2522.htm