checking the checkbox via jstl [duplicate]
Asked Answered
T

2

7

I have the following issue: when I put some data in model, I want on the view part check some checkboxes, that are equal to the field names in my object. here is html code:

<div class="myDiv">
    <div class="divClass">
           <input type="checkbox" name="someData" value="0" id="id1">
        <label for="id1">Field1</label>
    </div>
    <div class="divClass">
        <input type="checkbox" name="someData" value="1" id="id2">
        <label for="id2">Field2</label>
    </div>
    <div class="divClass">
        <input type="checkbox" name="someData" value="2" id="id3">
            <label for="id3">Field3</label>
    </div>
    <div class="divClass">
        <input type="checkbox" name="someData" value="3" id="id4">
        <label for="id4">Field4</label>
    </div>
</div>

and here is jstl pseudo_code, which I want to obtain:

<c:forEach var="field" items="${list.fields}">
    <c:if test="${field.name=='FIELD(1-4)'}">CHECK_THE_APPROPRIATE_CHECKBOX
    </c:if>
</c:forEach>
Triplicate answered 27/10, 2012 at 11:8 Comment(0)
L
16

To be checked, a checbox must have its checked attribute set (to "checked" if using XHTML). So the code could be something like the following:

<input type="checkbox" name="someData" value="2" id="id3" 
    <c:if test="${field.name == 'FIELD3'}">checked="checked"</c:if>
/>
Lodge answered 27/10, 2012 at 11:40 Comment(2)
A small typo: There's a > before </c:if>. :)Roane
it doesn't work... please provide the full code, which start from <c:forEach var="field" items="${list.fields}">Triplicate
L
-2

jstl pseudo_code For Checkbox

<c:set var="someData" value="${paramValues.someData}"></c:set>

<c:forEach var="i" items="${someData}">
            <c:set var="x" value="${x}${i},"></c:set>
            <br>     
</c:forEach>

<c:forTokens items="${x}" delims="," var="i" >`enter code here`
    <c:out value="Value : ${i }"></c:out>   
</c:forTokens>
Lathrop answered 2/8, 2017 at 16:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.