如何设置默认情况下,在Thymeleaf检查单选按钮

问题描述 投票:1回答:2

我有3个单选按钮组。用户可以只选择1 3.我要选择设置为默认单选按钮。我试过这个代码,但检查属性似乎被忽略。

    <div>
        <input type="radio" th:field="*{genre}" th:value="Strategy" checked="true">
        <label th:for="${#ids.prev('genre')}">Strategy</label>
    </div>
    <div>
        <input type="radio" th:field="*{genre}" th:value="RPG">
        <label th:for="${#ids.prev('genre')}">RPG</label>
    </div>
    <div>
        <input type="radio" th:field="*{genre}" th:value="Stealth">
        <label th:for="${#ids.prev('genre')}">Stealth</label>
    </div>

public class Game
{
    private String genre;
    //Constructor
    //Getter & Setter
}
spring thymeleaf
2个回答
2
投票

在你的控制器,设置genre = "Strategy"Game被添加到模型。


0
投票

使用JavaScript来解决这个问题。

<input type="radio" id="optionsRadios1" name="category" th:field="${post.category}" th:value="option1">

<script>
document.getElementById("optionsRadios1").setAttribute("checked","checked")
</script>

© www.soinside.com 2019 - 2024. All rights reserved.