2

I am trying to get my checkbox marked by setting an attribute of a model to true, but it does not get checked. This is my checkbox:

<div class="row text-center">
    <input type="hidden" value="on" name="_abroad"/>
    <input type="checkbox" name="abroad" onchange="getAbroad(this)"
           th:checked="${abroad} ? 'checked'"/>
    <span>Abroad</span>
</div>

and this is my model where I set my abroad property to true and the corresponing object I am adding to my model:

controller:

cityWrapper.setAbroad(true);
model.addAttribute("city", cityWrapper);

my wrapper:

public class CityWrapper {

    private List<City> cityList;
    private boolean abroad;
}
Mahozad
  • 18,032
  • 13
  • 118
  • 133
blaa
  • 801
  • 3
  • 16
  • 32

1 Answers1

0

You have to set it through the city object (set by the controller):

<div class="row text-center">
    <input type="hidden" value="on" name="_abroad"/>
    <input type="checkbox" name="abroad" onchange="getAbroad(this)"
           th:checked="${city.abroad}"/>
    <span>Abroad</span>
</div>

Please also refer to: Thymeleaf - How to add checked attribute to input conditionally

Mahozad
  • 18,032
  • 13
  • 118
  • 133