省份 城市 二级联动下拉框问题

Ami121 2008-06-17 09:07:15
我主要是这个意思 城市 省份是在A.jsp就是我在这个页面修改个人资料时候选择了相应的城市 省份 点击下一步进入到另外一个b.jsp页面后,然后我想点击上一步回到A.jsp页面 重新选择城市,当我回到A.jsp页面时,显示的就是我刚开始选择的省份 城市
...全文
610 点赞 收藏 22
写回复
22 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Ami121 2008-06-17
但是遨游 我试了还是不支持 至少我现在装的遨游不支持 加你了
回复
Landor2004 2008-06-17
这里的
onload
getElementById
innerHTML
似乎各大浏览器都支持

而firefox和IE两个浏览器都支持onbeforeunload 事件,傲游是ie内核呀,所以肯定支持
回复
Ami121 2008-06-17
感谢 Landor2004
回复
Landor2004 2008-06-17
只要你的代码符合w3c标准,就可以了
回复
Ami121 2008-06-17
这应该跟本身浏览器的内核有关系吧
回复
Landor2004 2008-06-17
手工记录一下selectIndex,测试通过,楼主可以参考一下




<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>aaa</title>

<script language=javascript>
window.onload = historyOncemore;
window.onbeforeunload = fixHistory;

var city=[
["北京","天津","上海","重庆","香港","澳门"],
["石家庄","唐山","秦皇岛","邯郸","邢台","保定","张家口","承德","沧州","廊坊","衡水"],
["太原","大同","阳泉","长治","晋城","朔州","晋中","运城","忻州","临汾","吕梁"],
["呼和浩特","包头","乌海","赤峰","通辽","鄂尔多斯","呼伦贝尔","巴彦淖尔","乌兰察布","兴安","锡林郭勒","阿拉善"]
]


function getCity(t){

alert(document.getElementById("province").selectedIndex)
//获得省份下拉框的对象
var sltProvince=document.userupdateForm.province;
//获得城市下拉框的对象
var sltCity=document.userupdateForm.city;

//得到对应省份的城市数组
var provinceCity=city[sltProvince.selectedIndex - 1];

//清空城市下拉框,仅留提示选项
// sltCity.length=1;

//将城市数组中的值填充到城市下拉框中
for(var i=0;i<provinceCity.length;i++){
sltCity[i+1]=new Option(provinceCity[i],provinceCity[i]);
}
}
function getSelfCity(t){
alert(document.getElementById("city").selectedIndex)
}

function fixHistory() //记住历史
{
document.getElementById("theHistoryRecord").value=document.getElementById("thedetailtableDIV").innerHTML.replace(/\n/g,"");
document.getElementById("index1").value = document.getElementById("province").selectedIndex;
document.getElementById("index2").value = document.getElementById("city").selectedIndex;
}

function historyOncemore() //恢复历史
{
if (document.getElementById("theHistoryRecord").value!="")
{
document.getElementById("thedetailtableDIV").innerHTML=document.getElementById("theHistoryRecord").value;
document.getElementById("province").selectedIndex = document.getElementById("index1").value;
document.getElementById("city").selectedIndex = document.getElementById("index2").value;
}
}

function submitProcess()
{
document.userupdateForm.submit();
}
</script>
</head>

<body>
<form name="userupdateForm" action="hehee.html">
<input type="button" value="完成" class="bottom" onclick="submitProcess();">
<div width=100% id="thedetailtableDIV">
<select id="province" name="province" onChange="getCity(this)">
<option value="" >请选择所在省份</option>
<option value="直辖市">直辖市</option>
<option value="河北">河北</option>
<option value="山西">山西</option>
</select>
<select id="city" name="city" onChange="getSelfCity(this)">
<option value="">请选择所在城市</option>
</select></td></tr>
</div>

</form>
<input name="theHistoryRecord" id="theHistoryRecord" type=hidden value="">
<input name="index1" id="index1" type=hidden value="">
<input name="index2" id="index2" type=hidden value="">
</body>

</html>

回复
Landor2004 2008-06-17
测试了一下,两个下拉框的内容在firefox和ie上都可以保存,

默认的selected在ie下是可以默认到的,但是在firefox上没默认出来,所以两个都是“请选择...”

但是里面的内容是对的,

再研究研究,呵呵
回复
Ami121 2008-06-17
可以在IE中返回没有问题 但是在遨游 火狐都只是显示出来 请选择所在省份 请选择城市
回复
Landor2004 2008-06-17
把document.all什么的都改成document.getElementById("xxx");


<input id="theHistoryRecord" name="theHistoryRecord" type=hidden value="">

看看
回复
xql80329 2008-06-17
up
回复
hornbilltofy 2008-06-17
参考http://hahaxiao.net/faces/website/website.jsp?id=14
回复
Ami121 2008-06-17
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


<title>updateuser</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

<script type="text/javascript">
window.onload = historyOncemore;
window.onbeforeunload = fixHistory;
//定义了城市的二维数组,里面的顺序跟省份的顺序是相同的。通过selectedIndex获得省份的下标值来得到相应的城市数组
var city=[
["北京","天津","上海","重庆","香港","澳门"],
["石家庄","唐山","秦皇岛","邯郸","邢台","保定","张家口","承德","沧州","廊坊","衡水"],
["太原","大同","阳泉","长治","晋城","朔州","晋中","运城","忻州","临汾","吕梁"],
["呼和浩特","包头","乌海","赤峰","通辽","鄂尔多斯","呼伦贝尔","巴彦淖尔","乌兰察布","兴安","锡林郭勒","阿拉善"]
];

function getCity(){
//获得省份下拉框的对象
var sltProvince=document.userupdateForm.province;
//获得城市下拉框的对象
var sltCity=document.userupdateForm.city;

//得到对应省份的城市数组
var provinceCity=city[sltProvince.selectedIndex - 1];

//清空城市下拉框,仅留提示选项
// sltCity.length=1;

//将城市数组中的值填充到城市下拉框中
for(var i=0;i<provinceCity.length;i++){
sltCity[i+1]=new Option(provinceCity[i],provinceCity[i]);
}
}

function fixHistory() //记住历史
{
document.all("theHistoryRecord").value=document.all("thedetailtableDIV").innerHTML.replace(/\n/g,"");
}

function historyOncemore() //恢复历史
{
if (document.all("theHistoryRecord").value!="")
{document.all("thedetailtableDIV").innerHTML=document.all("theHistoryRecord").value;
}
}

function selectallcheckbox(obj) //全选或全不选
{
var tureorfalse=obj.checked;
var theDetail=tbDetailUsed.rows;
for(var i=0;i<theDetail.length-1;i++)
{
theDetail[i].all("record_select").checked=tureorfalse;
}
}
function submitProcess()
{
document.userupdateForm.submit();
}

</script>
</head>
<body>
<table width="990" height="750">
<tr><logic:iterate id="user" name="userdata" scope="session">
<td width="200"><jsp:include page="/jsp/menu.jsp" flush="true"/></td>
<td align="center" valign="top"><table align="center">
<html:form action="/userupdate" method="post" onsubmit="return validateuserupdateForm(this)" focus="companyname">
<tr><td>用户名 <font color="red">*</font></td>
<td><html:text property="username" disabled="true" value="${user.udName}"/><FONT color="red"><html:errors property="username"/></FONT></td></tr>
<tr><td>请选择企业所在行业<font color="red">*</font></td>
<td><select name="business">
<option value="null" selected="selected">请选择</option>
<option value="医药卫生">医药卫生</option>
<option value="建筑建材">建筑建材</option>
<option value="冶金矿产">冶金矿产</option>
<option value="石油化工">石油化工</option>
</select><font color="red"><html:errors property="business"/></font></td>
</tr>
<tr><td>请选择企业所在城市<font color="red">*</font></td>
<td><div width=100% id=thedetailtableDIV><select name="province" onChange="getCity()">
<option value="null" selected="selected">请选择所在省份</option>
<option value="直辖市">直辖市</option>
<option value="河北">河北</option>
<option value="山西">山西</option>
<option value="内蒙古">内蒙古</option>

</select>
<select name="city">
<option selected="selected" value="null">请选择所在城市</option>
</select><font color="red"><html:errors property="city"/></font></td></tr></div>
<tr><td>请输入企业地址 </td>
<td><html:text property="companydress" value="${user.udCompdress}"/><font color="red"><html:errors property="companydress"/></font></td></tr>
<tr><td colspan="2" align="center">
<html:hidden property="status" value="update"/>
<html:hidden property="username" value="${user.udName}"/>
<html:submit property="update" value="提交更改" onclick="submitProcess();" />
<input name="theHistoryRecord" type=hidden value="">
</td></tr>
</html:form> </table>
</td></logic:iterate></tr>
</table>
<
<input name="theHistoryRecord" type=hidden value="">
</body>
</html>

我的jsp页面是这么修改的 可是显示不出来。
回复
Landor2004 2008-06-17
你的代码修改如下




<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>aaa</title>

<script language=javascript>
window.onload = historyOncemore;
window.onbeforeunload = fixHistory;

var city=[
["北京","天津","上海","重庆","香港","澳门"],
["石家庄","唐山","秦皇岛","邯郸","邢台","保定","张家口","承德","沧州","廊坊","衡水"],
["太原","大同","阳泉","长治","晋城","朔州","晋中","运城","忻州","临汾","吕梁"],
["呼和浩特","包头","乌海","赤峰","通辽","鄂尔多斯","呼伦贝尔","巴彦淖尔","乌兰察布","兴安","锡林郭勒","阿拉善"]
]


function getCity(){
//获得省份下拉框的对象
var sltProvince=document.userupdateForm.province;
//获得城市下拉框的对象
var sltCity=document.userupdateForm.city;

//得到对应省份的城市数组
var provinceCity=city[sltProvince.selectedIndex - 1];

//清空城市下拉框,仅留提示选项
// sltCity.length=1;

//将城市数组中的值填充到城市下拉框中
for(var i=0;i<provinceCity.length;i++){
sltCity[i+1]=new Option(provinceCity[i],provinceCity[i]);
}
}

function fixHistory() //记住历史
{
document.all("theHistoryRecord").value=document.all("thedetailtableDIV").innerHTML.replace(/\n/g,"");
}

function historyOncemore() //恢复历史
{
if (document.all("theHistoryRecord").value!="")
{document.all("thedetailtableDIV").innerHTML=document.all("theHistoryRecord").value;
}
}

function selectallcheckbox(obj) //全选或全不选
{
var tureorfalse=obj.checked;
var theDetail=tbDetailUsed.rows;
for(var i=0;i<theDetail.length-1;i++)
{
theDetail[i].all("record_select").checked=tureorfalse;
}
}
function submitProcess()
{
document.userupdateForm.submit();
}
</script>
</head>

<body>
<form name="userupdateForm" action="about:结果页面">
<input type="button" value="完成" class="bottom" onclick="submitProcess();">
<div width=100% id=thedetailtableDIV>
<select name="province" onChange="getCity()">
<option value="null" selected="selected">请选择所在省份</option>
<option value="直辖市">直辖市</option>
<option value="河北">河北</option>
<option value="山西">山西</option>
</select>
<select name="city">
<option selected="selected" value="null">请选择所在城市</option>
</select><font color="red"><html:errors property="city"/></font></td></tr>
</div>
</form>
<input name="theHistoryRecord" type=hidden value="">
</body>

</html>

回复
Ami121 2008-06-17
to Landor2004
采用你的方法 在ie下可以显示 但是遨游 火狐都不能显示 都只是显示出来 请选择所在省份 请选择城市
回复
Landor2004 2008-06-17
记住历史,并恢复历史

似乎可以这样

把你的内容放入一个div中然后
<script language=javascript>
window.onload = historyOncemore;
window.onbeforeunload = fixHistory;

function fixHistory() //记住历史
{
document.all("theHistoryRecord").value=document.all("thedetailtableDIV").innerHTML.replace(/\n/g,"");
}

function historyOncemore() //恢复历史
{
if (document.all("theHistoryRecord").value!="")
{document.all("thedetailtableDIV").innerHTML=document.all("theHistoryRecord").value;
}
}
</script>
<div id="thedetailtableDIV">你的内容</div>
<input name="theHistoryRecord" type=hidden value="">

回复
Landor2004 2008-06-17
最好是把你选择的省市id都放入session

当你点击上一步的时候,肯定是调用上一步的action,在action中根据省的id,再查询一回城市list

而当前选择的城市的id已经在session中,拿过来就可以
回复
laorer 2008-06-17
我是在b.jsp页面点击上一步 触发了window.history.go(-1)返回这个事件
-------------
这个似乎有点难度,我没有解决过,我觉得你可以直接用链接返回上一步
回复
Ami121 2008-06-17
<script type="text/javascript">
//定义了城市的二维数组,里面的顺序跟省份的顺序是相同的。通过selectedIndex获得省份的下标值来得到相应的城市数组
var city=[
["北京","天津","上海","重庆","香港","澳门"],
["石家庄","唐山","秦皇岛","邯郸","邢台","保定","张家口","承德","沧州","廊坊","衡水"],
["太原","大同","阳泉","长治","晋城","朔州","晋中","运城","忻州","临汾","吕梁"],
["呼和浩特","包头","乌海","赤峰","通辽","鄂尔多斯","呼伦贝尔","巴彦淖尔","乌兰察布","兴安","锡林郭勒","阿拉善"]
]


function getCity(){
//获得省份下拉框的对象
var sltProvince=document.userupdateForm.province;
//获得城市下拉框的对象
var sltCity=document.userupdateForm.city;

//得到对应省份的城市数组
var provinceCity=city[sltProvince.selectedIndex - 1];

//清空城市下拉框,仅留提示选项
// sltCity.length=1;

//将城市数组中的值填充到城市下拉框中
for(var i=0;i<provinceCity.length;i++){
sltCity[i+1]=new Option(provinceCity[i],provinceCity[i]);
}
}

</script>
</head>
<table>
<form name="userupdateForm">
<tr><td>请选择企业所在城市<font color="red">*</font></td>
<td><select name="province" onChange="getCity()">
<option value="null" selected="selected">请选择所在省份</option>
<option value="直辖市">直辖市</option>
<option value="河北">河北</option>
<option value="山西">山西</option>
</select>
<select name="city">
<option selected="selected" value="null">请选择所在城市</option>
</select><font color="red"><html:errors property="city"/></font></td></tr>
</table>
</table>

这是我的代码 我也知道1 2楼的意思 但是传值要怎么接收了 ?
3楼还没有明白我的意思.
我是在b.jsp页面点击上一步 触发了window.history.go(-1)返回这个事件
回复
胡矣 2008-06-17
没懂
如果你想保持开始选择的状态就保存A中的表单
如果不想保持就不要保存A中的表单
回复
guoxujie 2008-06-17
LZ需要那么麻烦吗?用到两jsp页面?
直接在一个jsp页面中用js函数来实现,当在一个下拉框里面选择了某个省,可以在另外的一个下拉框显示该省的所有城市。这样不就更简单吗?
回复
加载更多回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2008-06-17 09:07
社区公告
暂无公告