如何使用javascript隐藏jsp文件中的元素

How to hide an element in jsp file using javascript

本文关键字:元素 文件 jsp 何使用 javascript 隐藏      更新时间:2023-09-26
<li><label>Email:</label> <input type='text' name='username'
                            id="forgot_username" /></li>
                        <li><label>&nbsp;</label> <input type="submit"
                            id="forgot_btn" value="Send Reminder" class="btn"></li>
我有上面的文本框和按钮在我的jsp。下面的条件,我想要的是当这个条件发生时,上面的文本框和按钮应该隐藏我该怎么做呢
<c:if test="${param.message == 3}">
                        <span class="error"><font color="red"> Email does not match</font></span>
                    </c:if>

您应该像这样在第一个代码周围放置div标记:

<div id="hide"> 
    <ol>
        <li>
            <label>Email:</label>
            <input type='text' name='username' id="forgot_username" />
        </li>
        <li>
            <label>&nbsp;</label>
            <input type="submit" id="forgot_btn" value="Send Reminder" class="btn">
        </li>
    </ol>
</div>

然后你可以像这样隐藏:

<c:if test="${param.message == 3}">
    <script>
        $('#hide').hide();
    </script>
    <span class="error"><font color="red"> Email does not match</font></span>
</c:if>

为什么不能把这些<li>元素放在一个相同的结构中?

如果你不能把这些元素放在一起,又不想重复这个条件,你可以定义一个新的变量:

<c:set var="condition" value="0">
<c:if test="${param.message == 3}">
    <c:set var="condition" value="1">
</c:if>
<c:if test="${condition == 1}">
                        <span class="error"><font color="red"> Email does not match</font></span>
                    </c:if>
....
<c:if test="${condition == 1}">
<li><label>Email:</label> <input type='text' name='username'
                            id="forgot_username" /></li>
                        <li><label>&nbsp;</label> <input type="submit"
                            id="forgot_btn" value="Send Reminder" class="btn"></li>
</c:if>

这样,即使客户端禁用了JS或任何类型的JS问题,这些<li>也将被隐藏。