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

给你一囗甜甜゛ 提交于 2020-12-06 04:01:16

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!