Alex Alex - 26 days ago 6
jQuery Question

How to make one input type disable in HTML

I have a webapp, that implements CRUD functionality. The goal I'm trying to reach is to make Add editRow different from Edit. Here what I mean. When I invoke the Add function it makes a user capable(even mandatory) to provide an ID, whereas Edit disables a user of providing ID(or it would be saving a new entity, not updating). The simplest way to do that is to add "disable" for the input type. I've tried this:

<div class="form-group">
<label for="userId" class="control-label col-xs-3">ID</label>
<div class="col-xs-9">
<input type="number" class="form-control" id="userId" name="userId"
<c:choose>
<c:when test="${user.userId== null}"> placeholder="ID"
</c:when>
<c:otherwise>
placeholder = "${user.userId}" disabled </c:otherwise>
</c:choose>>
</div>
</div>


But this doesn't work.
My js scripts:

function add() {
form.find(":input").val("");
$("#editRow").modal();
}

function updateRow(id) {
$.get(ajaxUrl + id, function (data) {
$.each(data, function (key, value) {
form.find("input[name='" + key + "']").val(value);
});
$('#editRow').modal();
});
}


I don't find cool creating 2 editRow's with 1 difference

Answer Source

Try adding the disabled outside of the <c:otherwise> block. Like:

<div class="form-group">
<label for="userId" class="control-label col-xs-3">ID</label>
<div class="col-xs-9">
<input disabled type="number" class="form-control" id="userId" name="userId" 
<c:choose>
<c:when test="${user.userId== null}"> placeholder="ID" 
</c:when> 
<c:otherwise> 
placeholder = "${user.userId}"</c:otherwise>
</c:choose>>
</div>
</div>