【问题标题】:Modify class of a button tag in a JSP on the basis of EL value根据EL值修改JSP中按钮标签的类
【发布时间】:2018-10-23 09:51:53
【问题描述】:

我在 JSP 中的按钮标签是

<button type="submit" class="btn btn-danger student-age">
<span class="age">${student.age}</span>
</button>

如果学生年龄大于 18,我希望我的按钮类 btn-danger 更改为 btn-success。

student.age 来自使用 EL 的 java 代码。

【问题讨论】:

    标签: jsp el


    【解决方案1】:

    我会选择三元表达式:

    <button type="submit" class="btn ${student.age gt 18 ? 'btn-success' : 'btn-danger'} student-age">
    <span class="age">${student.age}</span>
    </button>
    

    或者如果您需要大于或等于,请使用ge 而不是gt

    另见:

    【讨论】:

    • 太棒了,不知道在课堂上可以这样使用
    【解决方案2】:

    使用 JSTL ltgt(小于和大于)

    <c:if test="${student.age gt 18 || student.age eq 18}">
    <button type="submit" class="btn btn-success student-age"><span class="age">${student.age</span></button>
    </c:if>
    
    <c:if test="${student.age lt 18}">
    <button type="submit" class="btn btn-danger student-age"><span class="age">${student.age</span></button>
    </c:if>
    

    【讨论】:

    • 谢谢伙计,我改用三元表达式了:)
    • 如果年龄等于 18,这将失败
    • 啊,好点。已编辑。无论如何,您的解决方案要简单得多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 2013-07-20
    • 2016-06-23
    • 2020-09-07
    相关资源
    最近更新 更多