如何在 Play Framework 中隐藏文本字段?例如如何隐藏此字段:
@inputText(userProfileForm("name"), '_label -> "Name")
最佳答案
这应该适用于所有浏览器:
@inputText(
userProfileForm("name"),
'_label -> "Name",
'style -> "display: none"
)
请注意,这只会隐藏字段,而不是标签等。
如果您还想隐藏标签,则无法使用默认助手执行此操作。
但是,您可以自己指定输入字段:
<input type="hidden" name="name" value="@userProfileForm.data.get("name")" />
name
应该是表单中的字段名称(巧合 name
在这种情况下也是如此)。我还没有测试过
value
但它应该工作,也许你需要剥离 "
约name
.如果这样做,隐藏的数据将与表单中的其他数据一起发送到服务器。
编辑:
如果隐藏的值是空的,这意味着你在调用 View 时没有将它绑定(bind)到表单。您可以在 Java 中将名称绑定(bind)到这样的形式(我不知道 Scala,但它已解释为 in the Play Scala documentation):
Map<String, String> data = new HashMap<>();
data.put("name","get the username here");
return ok(index.render(userProfileForm.bind(data));
另一种选择(在我看来更简洁)是简单地将用户名作为参数传递给 View 。 Controller 变为:
return ok(index.render(userProfileForm, "username goes here"));
然后在 View 中您可以简单地执行以下操作:
@(userProfileForm : Form[UserProfileForm])(name : String)
@helper.form(...){
<input type="hidden" name="name" value="@name" />
//...
}
关于scala - 如何在 Play Framework 中隐藏文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16911393/