have razor text box default value disappear

纵然是瞬间 提交于 2019-12-23 02:53:15

问题


I have a form with two text boxes. What I would like is when the user clicks in the text boxes the default values disappears. I am using razor so not sure how to add the onfocus event I think I need.

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = "newsletterform" }))
{
    @Html.TextBoxFor(m => m.Name, new { @Value = "Name"})

    @Html.TextBoxFor(m => m.Email, new { @Value = "Email"})

    <input type="submit" class="newsletterGo" value="Go" />
}  

回答1:


You can user the placeholder attribute. An example of it is the search box at the top of this page

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = "newsletterform" }))
{
    @Html.TextBoxFor(m => m.Name, new { placeholder = "Default Name"})

    @Html.TextBoxFor(m => m.Email, new { placeholder = "person@example.com"})

    <input type="submit" class="newsletterGo" value="Go" />
}  

Also, you don't need to specify the @Value attribute. The HTML helpers take care of setting the input values for you.



来源:https://stackoverflow.com/questions/8433234/have-razor-text-box-default-value-disappear

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!