日期:2014-05-18  浏览次数:20681 次

请教一个jsp循环的问题[菜鸟提问】
想做一个根据下拉框中的选择个数,显示文本框的个数,但是总是有问题(偶是菜鸟,请大家多多关照)!
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!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>无标题文档</title>
</head>
<%!int bb;%>
<body>
<form action="" name="myform">
<table width="780" border="0" cellspacing="1" cellpadding="0">
  <tr>
  <td><div align="center"><span class="style2">Please choose the parameter of the period: Year of Period</span>
  <input type="text" name="t" value="2009"/>


NO

<select name="aa" onchange="myform.submit()"> 
<option value="" selected>please choose</option> 
<option value=1>1</option> 
<option value=2>2</option> 

</select>
  
  </div></td>
  </tr>
</table>
</form>

<%bb=request.getParameter("aa");%>
<%
for(int i=1;i<=bb;i++){%>
<%=i%>
<%
}
%>


</body>
</html>


谢谢啦!!

------解决方案--------------------
HTML code
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %> 
<!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>无标题文档 </title> 
</head> 
<%int bb=0;%> 
<body> 
<form action="" name="myform"> 
<table width="780" border="0" cellspacing="1" cellpadding="0"> 
  <tr> 
    <td> <div align="center">&nbsp; <span class="style2">Please choose the parameter of the period: Year of Period </span> 
        <input type="text"  name="t" value="2009"/> 


NO 

<select name="aa"  onchange="myform.submit()"> 
<option  value="" selected>please choose </option> 
<option  value=1>1 </option> 
<option  value=2>2 </option> 

</select> 
  
    </div> </td> 
  </tr> 
</table> 
</form> 

<%
String cc = request.getParameter("aa");
if(cc!=null&&!cc.equals(""))
bb=Integer.valueOf(cc).intValue();System.out.println(bb);%> 

<% 
for(int i=1;i <=bb;i++){%> 
 <input type="text" name="a<%=i%>">
<% 
} 
%> 


</body> 
</html>

------解决方案--------------------
问题出在这:
bb=request.getParameter("aa");

1)request.getParameter返回的是个string,bb是int型。必须要转换。
2)第一次进入画面时 request.getParameter("aa")是null。需要特殊处理