如何在播放框架中隐藏文本字段 [英] How to hide a text field in play framework

查看:68
本文介绍了如何在播放框架中隐藏文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在播放框架中隐藏文本字段?例如,如何隐藏此字段:

How to hide a text field in play framework? For example how to hide this field:

@inputText(userProfileForm("name"), '_label -> "Name")

推荐答案

这在所有浏览器中均应适用:

This should work in all browsers:

@inputText(
    userProfileForm("name"),
    '_label -> "Name",
    'style -> "display: none"
)

请注意,这只会隐藏字段,不会隐藏标签等.

Note that this only hides the field, not the label etc.

如果还要隐藏标签,则无法使用默认助手. 但是,您可以自己指定输入字段:

If you want to hide the label aswell, you can't do this with the default helpers. You can however specify the input field yourself:

<input type="hidden" name="name" value="@userProfileForm.data.get("name")" />

name应该是表单中字段的名称(在这种情况下,巧合的是name).
我尚未测试value,但是它应该可以工作,也许您需要在name周围剥离".

The name should be name of the field in your form (coincidentally name aswell in this case).
I haven't tested the value but it should work, maybe you'll need to strip the " around name.

如果执行此操作,则隐藏的数据将与表单中的其他数据一起发送到服务器.

If you do this, the hidden data will be sent, along with the other data in the form to the server.

修改:
如果隐藏值为空,则表示您在调用视图时未将其绑定到表单.您可以在Java中将名称绑定到这样的表单(我不知道Scala,但是在


If the hidden value is empty, that means you didn't bind it to the form when calling the view. You can bind the name to the form like this in Java (I don't know Scala, but it's explained 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));

另一个选择(我认为更干净)是将用户名作为参数传递给视图.控制器变为:

Another option (which is cleaner in my opinion) is to simply pass the username as an argument to the view. The controller becomes:

return ok(index.render(userProfileForm, "username goes here"));

然后您可以在视图中简单地执行以下操作:

And in the view you can then simply do:

@(userProfileForm : Form[UserProfileForm])(name : String)

@helper.form(...){
    <input type="hidden" name="name" value="@name" />
    //...
}

这篇关于如何在播放框架中隐藏文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆