如何设置Thymeleaf中默认选中的单选按钮

时间:2018-09-19 16:39:02

标签: spring thymeleaf

我有3个单选按钮组。用户只能选择3个中的1个。我想设置一个单选按钮,它被选为默认按钮。我已经尝试了这段代码,但是 checked 属性似乎被忽略了。

    <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
}

2 个答案:

答案 0 :(得分:1)

在您的控制器中,将genre = "Strategy"添加到模型后,设置Game

答案 1 :(得分:0)

使用JavaScript来解决此问题。

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

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

相关问题