开发者

Can I use a jsp tag to hide an input field on load

I need to hide a field on page load based on the va开发者_开发知识库lue 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?


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'}">


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>


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜