首页 > 解决方案 > 如何在 JSP 中获取输入值?

问题描述

我有一个 JSP 页面需要读取 a 中的输入值if,我尝试使用 Jquery 但不起作用。

<%-- my input with the year value --%>    
<input type="text" id="year" value="2021">

<%-- my JSP page need test if year is bigger then 2019 --%>
<c:if test="${ $("#year") > 2019 }">
    ...
</c:if>

欢迎任何帮助。

标签: jqueryjsp

解决方案


我可以通过 3 个步骤解决这个问题:

首先,我在 ajax 参数中发送输入的值

year    : $("#year").val()    

在我收到 Servlet 中的值之后

String year_value = request.getParameter("year");

并设置一个属性来获取 JSP 页面中的值。

request.setAttribute("year_value", Integer.parseInt(year_value));

在 JSP 页面中,我使用变量year_value进行比较if

<c:if test="${ year_value > 2019 }">
    ...
</c:if>

推荐阅读