组合框中的百里香为默认值

时间:2018-10-31 15:19:10

标签: html5 winforms combobox frontend thymeleaf

我的胸腺叶有问题。我有一个带有组合框和一些字段的表单。我想将默认值放到组合框中,并且对“ selected”这个选项不满意。

代码是这样的:

<select class="dsp-inline form-control" th:field="*{tipoDocumento}" required="required" th:disabled="${permisoXestion == false}">
      <option value="" th:text="#{select.option.default}"> </option>
      <option th:each="row : ${tipoDocumento}" th:value="${row}" th:text="#{${row.value}}" th:selected="#{${row==2}}"></option>
</select>

其中“ tipoDocumento”是具有两个值的枚举:

public enum TipoDocumento {
 PUBLICO("documento.tipo.publico"),
 PRIVADO("documento.tipo.privado");

private String property;

private String value;

private TipoDocumento(String property) {

    this(property, null);

    }

private TipoDocumento(String value, String property) {
   this.value = value;
   this.property = property;

}

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

public String getProperty() {
    return property;
}

public void setProperty(String property) {
    this.property = property;
}
}

有人可以帮我吗?

2 个答案:

答案 0 :(得分:1)

所选标签应该可以使用。请记住,它需要像selected="selected"一样添加。我已经在几个选择上使用了它,并且它总是可以工作。另外,在您的th:each中,您需要删除th:selected="#{${row==2}}"元素,否则您的第一个选项将不是默认选项。

<select class="dsp-inline form-control" required="required" th:disabled="${permisoXestion == false}">
      <option value="" th:text="#{select.option.default}"></option>
      <option th:each="row, iter : ${tipoDocumento}" th:value="${row}" th:text="#{${row.value}}" th:selected="${iter.count eq 2} ? 'selected' : 'false'"></option>
</select>

答案 1 :(得分:0)

如果使用th:selected,则不能使用th:field。您必须将其替换为name属性。 因此,您可以替换

<select class="dsp-inline form-control" th:field="*{tipoDocumento}" required="required" th:disabled="${permisoXestion == false}">

 <select class="dsp-inline form-control" name="tipoDocumento" required="required" th:disabled="${permisoXestion == false}">

此外,th的条件:已选择 可以替换

th:selected="#{${row==2}}"

th:selected="${row==2}"
相关问题