0

.jsp 文件

<html>
<body>
<form method="post" action="index.jsp" />

<% 
String integer = request.getParameter("integer"); 
%>

<%
int x = 0;
try { x = Integer.parseInt("integer"); }
catch (Exception e) { x = 0; }
%>

<input type="text" name="integer" value="<%=x%>"/>
<input type="submit" value="submit" />

</form>
</body>
</html>

每次点击提交按钮时,如何添加或增加 x 输出?

4

2 回答 2

1
Integer.parseInt("integer"); -> "integer" string is not a number therefore it will be a wrong format.

并且您每次都在初始化 x = 0 并且 x 没有增量

你可以试试这个:

<form method="post" action="" />

<%
  String integer = request.getParameter("integer");          
  int x = integer != null ? Integer.parseInt(integer) : 0;      
  ++x;
%>

<input type="text" name="integer" value="<%=x%>"/>
<input type="submit" value="submit" />
于 2013-03-05T04:38:40.370 回答
0
<html>
<body>
<form method="post" action="index.jsp" />

<% 
String integer = request.getParameter("integer"); 
%>

<%
int x = 0;
try { x = Integer.parseInt("integer"); }
catch (Exception e) { x = 0; }
x = x + 1;
%>

<input type="text" name="integer" value="<%=x%>"/>
<input type="submit" value="submit" />

</form>
</body>
</html>
于 2013-03-05T02:33:17.880 回答