2011-07-09 19 views
5

如何通过检查我的jsp中的条件来禁用按钮?如果为true,则该按钮被启用,如果为false,则该按钮被禁用。该条件将检查变量的值。我知道如何禁用使用JavaScript的按钮,但将它与jsp中的条件一起使用是我无法弄清楚的。这是否可能?如何根据jsp中的条件禁用按钮?

回答

6

尝试使用JSTL结构是这样的:

<input type="button" <c:if test="${variable == false}"><c:out value="disabled='disabled'"/></c:if>"> 

更多的例子见http://www.ibm.com/developerworks/java/library/j-jstl0211/index.html

+1

无需假比较。只需使用$ {!variable}。并且不需要使用c:out来输出不需要HTML转义的静态文本。 –

2

我的做法是这样的:

<c:choose> 
    <c:when test="${condition == true}"> 
     <input type="button" disabled="disabled"/> 
    </c:when> 
    <c:otherwise> 
     <input type="button" /> 
    </c:otherwise> 
</c:choose> 
3

或者干脆你可以使用el做直接像这样:

<input type="button" ${ condition ? 'disabled="disabled"' : ''}/> 

举个例子:

<input type="button" ${ someVariable eq 5 ? 'disabled="disabled"' : ''}/> 
相关问题