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输出?
答案 0 :(得分: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" />
答案 1 :(得分: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>