您使用的 Play 表单元素是用于生成纯 html 的 Play Scala 模板。
出于您的目的,我认为您应该像使用任何 html 表单一样使用 javascript。一个例子是:
<script type="text/javascript">
window.onload = function(e) {
document.getElementById("myCheck").onclick = function() {
showHideElement();
};
showHideElement();
}
function showHideElement() {
var checked = document.getElementById("myCheck").checked;
var el = document.getElementById("myOptionValue");
if (checked) {
el.style.display = 'block'
} else {
el.style.display = 'none'
}
}
</script>
您的复选框和输入文本模板都应添加 id 属性,例如:
@checkbox(myForm("isEnabled"), 'id -> "myCheck")
@inputText(myForm("someOptionvalue"), 'id -> "myOptionValue")
仅供参考,您可以在上面的标签中添加更多属性,例如 CSS 样式类,例如:
@inputText(myForm("name"),
'id -> "username",
'class -> "classForInputText",
'_id -> "idForTheTopDlElement",
'_class -> "classForTheTopElement",
'size -> 30
)
然后您可以将显示设置为无或阻止