我是 JSP 和 java 新手。我想执行一段代码,如果按下复选框,该代码将显示或隐藏内容。我是这样想的:如果按下复选框, boolean 变量将变为 true。如果 boolean 变量为 true,则执行另一个 if 语句。
但在我的最终结果中,我的所有内容都被隐藏了。有人可以向我解释为什么会发生这种情况,或者给我另一种方法吗?
<%boolean hide = false;
String strCheckBoxValue= (String) session.getAttribute("checkweekends");
if (strCheckBoxValue != null) {
hide = true;
} %>
<c:if test="${hide == true}">
<c:if test="${a.dateFormat_hideweekends(day+z) !=''}">
<c:if test="${a.getData(time,day+z, sala)!=''}">
<TD align="center" valign="middle" width="100" title="${a.getData(time,day+z, sala)}" style="color:#00008B;text-overflow: ellipsis;width: 200px;white-space: nowrap;overflow: hidden;" bgcolor="#FFFF00">
<c:out value="${a.getData(time,day+z, sala)}" />
</TD>
</c:if>
<c:if test="${a.getData(time,day+z, sala)==''}">
<TD align="center" valign="middle" width="100" bgcolor="#90EE90">
</TD>
</c:if>
</c:if>
</c:if>
最佳答案
这里的问题是您对这段代码的作用有一个根本性的误解。您的 Java
代码在服务器端执行。当用户单击复选框时,运行的代码将在客户端运行。
您无法让 Java 代码在客户端工作,因为它是服务器端语言。这是像 Javascript 这样的客户端语言的工作。例如..
<input type="checkbox" onchange="eventHook()"/>
<script type='text/javascript'>
function eventHook() {
// This code is ran when the checkbox is changed.
}
</script>
关于java - 如果在 JSP 中选中复选框,则显示/隐藏内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28439304/