当前位置: 代码迷 >> Web前端 >> struts使用上拉列表框
  详细解决方案

struts使用上拉列表框

热度:305   发布时间:2012-09-22 21:54:54.0
struts使用下拉列表框

1.使用<html:option>标签

 <html:select property="city">
  <html:option value="010">北京市</html:option>
  <html:option value="020">广州市</html:option>
  <html:option value="021">上海市</html:option>
 </html:select>
 


2.使用<html:optionsCollection>标签
1
)在ActionForm中加入字段
???? private List cityList=new ArrayList();
2
)加入必须的getter方法
public List getCityList() {
?? return cityList;
}
3
)在reset方法内给cityList填充内容
???? cityList.add(new LabelValueBean("
北京市","010"));
???? cityList.add(new LabelValueBean("
广州市", "020"));
???? cityList.add(new LabelValueBean("
上海市", "021"));
4
JSP标签中使用
<html:select property="city2">
?? <html:optionsCollection property="cityList" label="label" value="value"/>
</html:select>

3.使用<html:options>标签

 <%List cityList=new ArrayList();
cityList.add(new LabelValueBean("北京市","010"));
cityList.add(new LabelValueBean("广州市", "020"));
cityList.add(new LabelValueBean("上海市", "021"));
request.setAttribute("list",cityList); //必须是request对象
%>
 <html:select property="city3">
  <html:options collection="list" labelProperty="label" property="value"/>
 </html:select>
 
<%@ page contentType="text/html;charset=gb2312" %>
<html>
<body>
<TITLE>级联菜单</TITLE>
<STYLE type="text/css">
td{font-size:12px;line-height:22px;color:#FFFFFF;}
div{font-size:12px;line-height:22px;}
.red {color: #FF0000}
</STYLE>
<script language="JavaScript" >
<!--下拉框级联开始-->
var cityList = new Array( ); //定义城市数组
cityList['大众'] = ['甲壳虫','迈腾','高尔夫', '捷达'];
cityList['奔驰'] = ['奔驰C','奔驰D','奔驰R', '奔驰G'];
cityList['现代'] = ['跃动', '雅尊', '途胜'];
cityList['本田'] =['思域', '雅阁', '飞渡'];
cityList['沃尔沃'] = ['沃尔沃C30', '沃尔沃C70', '沃尔沃S40', '沃尔沃S80'];
function changeCity( )
{
var province=document.myform.province.value;
document.myform.city.options.length=0;
for (var i in cityList)
{
if (i == province)
{
for (var j in cityList[i]) //根把对应城市动态创建区域option
{
document.myform.city.options.add(new Option(cityList[i][j], cityList[i][j]));
}
}
}
document.myform.city.options.selctIndex=0;
}

function loadAll( ) //动态创建城市option
{ for (var i in cityList)
{ document.myform.province.options.add(new Option(i, i));
}
document.myform.province.selectedIndex = 0;
}
<!--下拉框级联结束-->
</SCRIPT>
</HEAD>

<BODY leftmargin="0" topmargin="0" onLoad="loadAll( )"><DIV>
<TABLE width="760" border="0" cellspacing="0" cellpadding="0" align="center">
<TR><TD height="5"></TD></TR>
<FORM action="" method="post" name="myform">
<TR>
<TD HEGIHT="30" bgcolor=red>? <B>车型搜索:</B>?
<SELECT name="province" id="province" onChange="changeCity()">
<OPTION>车名</OPTION></SELECT>
<SELECT name="city" id="city" >
<OPTION>车系</OPTION>
</SELECT>
<SELECT ><OPTION> 车型</OPTION>
<OPTION> 豪华</OPTION>
<OPTION> 标配</OPTION>
<OPTION> 敞篷</OPTION>
</SELECT>
<INPUT name="b1" type="submit" value=" 查 询 "></TD>
</TR></FORM>
<TR><TD height="5"></TD></TR>
</TABLE></DIV>
</BODY>
</HTML>
  相关解决方案