问题
I need to hide a field on page load based on the value of a request attribute. I don't want a 'hidden' field because I want to show it again. I don't want to do this with javascript. How is this done with jsp tags?
回答1:
Set a condition where display is block if the condition is true. Else if the condition is false set the display to none.
<c:set var="inputDisplay" value="1" /> <!-- This same as your request attribute -->
<c:choose>
<c:when test="${inputDisplay == 1}">
<input type="text" />
</c:when>
<c:otherwise>
<input type="text" style="display:none;" />
</c:otherwise>
</c:choose>
回答2:
Use the conditional operator in EL.
<div class="${hide ? 'hide' : 'show'}">
where ${hide}
is the request attribute evaluating to a boolean
. If it evaluates true
, then the class name "hide"
will be printed, else the class name "show"
will be printed.
Of course define those classes in your stylesheet as well.
.hide { display: none; }
.show { display: block; }
No need for JSTL tags here.
Or if you don't want to use CSS class definitions for some unobvious reason, then do
<div style="display:${hide ? 'none' : 'block'}">
回答3:
The following code will only show include the code between the tags if requestAttribute
evaluates to true
to have the opposite effect use ${not requestAttribute}
instead.
<c:if test="${requestAttribute}">
//Code here
</c:if>
来源:https://stackoverflow.com/questions/5656372/can-i-use-a-jsp-tag-to-hide-an-input-field-on-load