如何从选项处于循环中的选择框中获取值?

时间:2016-01-06 10:39:21

标签: html jsp

我正在制作一个带日期的选择框,所以我为这样的选项创建了一个循环:

<select name="BDay" /> 
<%
    for (int i = 1; i <= 31; i++) {
        out.print("<option value=" + i + ">");
        out.print(i);
        out.print("</option>");
    } %> 
        </select>
         <select name="BMonth" /> 
    <%
        for (int j = 1; j <= 12; j++) {
            out.print("<option value=" + j + ">");
            out.print(j);
            out.print("</option>");
        }
     %> 
    </select> 
    <select name="BYear" /> 
    <%
        for (int k = 1915; k <= 2011; k++) {
            out.print("<option value=" + k + ">");
            out.print(k);
            out.print("</option>");
        }
     %> 
    </select>

然后在另一个页面中,当我尝试获取用户选择的信息时(如下所示):

int BDay = Integer.parseInt(request.getParameter("BDay"));
int BMonth = Integer.parseInt(request.getParameter("BMonth"));
int BYear = Integer.parseInt(request.getParameter("BYear"));

我收到此错误:     java.lang.NumberFormatException:null

有人可以帮忙吗? 提前谢谢!

1 个答案:

答案 0 :(得分:0)

如果没有自动关闭代码,代码将显示为

<select name="BDay" > 
<%
    for (int i = 1; i <= 31; i++) {
        out.print("<option value=" + i + ">");
        out.print(i);
        out.print("</option>");
    } %> 
</select>
<select name="BMonth" > 
<%
    for (int j = 1; j <= 12; j++) {
        out.print("<option value=" + j + ">");
        out.print(j);
        out.print("</option>");
    }
 %> 
</select> 
<select name="BYear" > 
<%
    for (int k = 1915; k <= 2011; k++) {
        out.print("<option value=" + k + ">");
        out.print(k);
        out.print("</option>");
    }
 %> 
</select>

详细了解serf-closing tags Are self-closing tags valid in HTML5?