0

In my template Thymeleaf in form:

<ul name="property" id="property" class="form-control mt-3">
                        <li th:each="property, el : ${tabs}">

                            <input type="checkbox" checked th:id="${el.count}" th:value="*{property.id}" th:field="*{shop.listOfProperty}" />
                            <label 
                                   th:text="${property.name}">Heating</label>
                        </li>
                    </ul>
  • ${tabs} - list objects : List
  • *{shop.listOfProperty} - list object in my object Shop : Shop.listOfProperty - List

In result:

<ul name="property" id="property" class="form-control mt-3">
                        <li class="" >

                            <input type="checkbox" id="1" value="1" name="listOfProperty" /><input type="hidden" name="_listOfProperty" value="on"/>
                            <label>FOOD</label>
                        </li>
                        <li class="" >

                            <input type="checkbox" id="2" value="2" name="listOfProperty" /><input type="hidden" name="_listOfProperty" value="on"/>
                            <label>CLOTHES</label>
                        </li>
                        <li class="" >

                            <input type="checkbox" id="3" value="3" name="listOfProperty" /><input type="hidden" name="_listOfProperty" value="on"/>
                            <label>BUILDING</label>
                        </li>
                    </ul>

I can't set a check for my checkbox in any way. I tried: checked, th:checked

I can to set checked if I not use th:field :

    <li >
  <input type="checkbox"  th:id="${tabs[0].id}" checked="checked" th:value="${tabs[0].id}"  />
                                <label
                                        th:text="${tabs[0].name}">Heating</label>
                            </li> 
Serg
  • 77
  • 1
  • 6
  • 1
    Change `checked` to `checked = "checked"` – Faheem azaz Bhanej Dec 06 '21 at 06:52
  • Does this answer your question? [Thymeleaf - How to add checked attribute to input conditionally](https://stackoverflow.com/questions/29826576/thymeleaf-how-to-add-checked-attribute-to-input-conditionally) – xerx593 Dec 06 '21 at 07:23
  • https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html#checkbox-fields – xerx593 Dec 06 '21 at 07:30
  • if I use the tag th:field then I can't set a check in any way. But if Not use the tag th:field then I can't receive value on back – Serg Dec 06 '21 at 08:00
  • use th:field and property value must be set to 1,2,3 so proper checkbox will be checked accordingly – Antoniossss Dec 06 '21 at 09:07
  • @Antoniossss do not quite understand. can you explain in more detail? – Serg Dec 06 '21 at 10:00

0 Answers0