JQuery不会使用数据自动填充字段元素

时间:2012-03-20 20:21:54

标签: c# jquery model-view-controller

我有以下代码:

@Html.TextBoxFor(x => x.Name, new { @onload = "if(this.value=='')this.value='NAME';", @class = "text", @onblur = "if(this.value=='')this.value='NAME';", @onfocus = "if(this.value=='NAME')this.value='';" })
@Html.TextBoxFor(x => x.Email, new { @onload = "if(this.value=='')this.value='EMAIL ADDRESS';", @class = "text", @onblur = "if(this.value=='')this.value='EMAIL ADDRESS';", @onfocus = "if(this.value=='EMAIL ADDRESS')this.value='';" })

但是当点击框时它只显示'NAME',我希望它在那里开始。

2 个答案:

答案 0 :(得分:3)

为什么不直接使用HTML5占位符属性?这似乎是你想要做的事情。

@Html.TextBoxFor(x => x.Name, new { placeholder = "NAME" })
@Html.TextBoxFor(x => x.Email, new { placeholder = "EMAIL ADDRESS" })

答案 1 :(得分:0)

您可以使用HTML5占位符和插件jquery-placeholder的属性与其他浏览器兼容

Html + Razor:

@Html.TextBoxFor(x => x.Name, new { placeholder = "NAME", @class="with-placeholder" })
@Html.TextBoxFor(x => x.Email, new { placeholder = "EMAIL ADDRESS", @class="with-placeholder"})

JS:

$('.with-placeholder').placeholder();

来自html5please.com

的数据