根据值启用和禁用引导按钮

时间:2015-08-28 18:32:16

标签: jquery

我正在尝试根据值禁用并启用bootstrap按钮。 这是我的代码:

<table id="userTable" class="table table-striped">
    <thead>
        <tr>
            <th>Lastname</th>
            <th>Firstname</th>
            <th>Email</th>
        </tr>
    </thead>
    <tbody>
        <c:forEach var="address" items="${addresses.userAddresses}" varStatus="loop">
            <tr>
                <td>
                    <c:out value="${address.lastName}" />
                </td>
                <td>
                    <c:out value="${address.firstName}" />
                </td>
                <td>
                    <c:out value="${address.address}" />
                </td>
                <td>
                    <c:out value="${address.isUpdateable}" />
                </td>
                <td>
                    <button id="btnbutton" onclick="location.href='/project/update/${loop.index}'" type="button" class="btn btn-info">Edit</button>
                    <form:form method="post" modelAttribute="address" action="/project/delete/${loop.index}">
                        <button type="submit" class="btn btn-danger" data-toggle="modal" data-title="Delete Address" onclick="return confirm('Are you sure you want to delete this address?');">Delete</button>
                    </form:form>
                </td>
            </tr>
        </c:forEach>
    </tbody>
</table>

基于$ {address.isupdateable}值,我必须启用/禁用编辑按钮。我尝试了很多方法,我使用了disabled属性,我使用了jquery函数,但没有任何工作。任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:0)

使用javascript,您可以尝试类似

的内容
<button id="btnbutton" onclick="location.href='/project/update/${loop.index}'" type="button" class="btn btn-info" style="visibility: hidden">Edit</button>

作为按钮标记的属性。观察style="visibility: hidden"属性

使用Javascript并根据您的要求,您可以使用

document.getElementById("btnbutton").style.visibility="hidden"

要启用,请使用

document.getElementById("btnbutton").style.visibility="visible"

希望这会有所帮助......

更新 - 1:

试试这个..它必须有效

用以下内容替换按钮:

<c:choose>
    <c:when test= "${address.isupdateable.equals("true")}">
        <button id="btnbutton" onclick="location.href='/project/update/${loop.index}'" type="button" class="btn btn-info">Edit</button>
    </c:when>
<c:otherwise>
    <button id="btnbutton" onclick="location.href='/project/update/${loop.index}'" type="button" class="btn btn-info">Edit</button>>Edit</button>
</c:otherwise>

答案 1 :(得分:0)

$(document).ready(function() {
if(${address.isUpdateable}=true){ //update this with whatever your rule is
$('#btnbutton').prop('disabled', true); //change the ID to disable whatever button you want.
}
});

不确定${address.isUpdateable}可以包含哪些值(文本或数字)。因此,您可能还需要将此值另存为变量,然后在逻辑/ if语句中使用它。

答案 2 :(得分:0)

您可以使用 Jquery 中的 onLoad 功能。如果已禁用或已启用,请阅读您想要使用的calue,这是一个示例:

$(function() {
    var isupdateble = ${address.isUpdateable}; //here get your value!
    if(!isupdateble){
        $("#btnbutton").attr("disabled", "disabled");
    }
});

或者在jsFiddle

中测试一下
相关问题