我有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
}
在你的控制器,设置genre = "Strategy"
当Game
被添加到模型。
使用JavaScript来解决这个问题。
<input type="radio" id="optionsRadios1" name="category" th:field="${post.category}" th:value="option1">
<script>
document.getElementById("optionsRadios1").setAttribute("checked","checked")
</script>