asp.net-mvc - Asp.Net Mvc - Html.TextBox - 设置自动对焦属性

标签 asp.net-mvc .net-3.5 html-helper

在 Html 5 中,文本框上有一个名为 autofocus 的新属性。

问题在于它是一个 bool 值(存在或不存在)

它应该看起来像:

<input name="a" value="" autofocus>

我尝试过:

<%= Html.TextBox( "a", null, new { autofocus } ) %>

但是,它给了我一个错误,因为我没有设置自动对焦的值......

我知道我可以手动完成,但是我可以使用 Html.TextBox 完成吗?

最佳答案

尝试<%= Html.TextBox( "a", null, new { autofocus = "" } ) %>

根据HTML5 spec on boolean attributes :

If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.

所以要么

  • <input name="a" value="" autofocus>
  • <input name="a" value="" autofocus="">
  • <input name="a" value="" autofocus="autofocus">

应该是有效的。

关于asp.net-mvc - Asp.Net Mvc - Html.TextBox - 设置自动对焦属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2998247/

相关文章:

asp.net-mvc - Asp.Net MVC 模型与 JQWidgets 绑定(bind)

c# - 在 MVC 网页中显示一个计数器

.net - 将 WPF 应用程序分发给旧用户群 : How seamless is it?

asp.net-mvc - 您是否使用任何自定义 ASP.NET MVC HtmlHelper 扩展?

asp.net-mvc - 如何使用Moq对HtmlHelper进行单元测试?

asp.net - 如何在 ASP.NET MVC 母版页上注入(inject) HTML

c# - Entity Framework +验证注解

.net - .Take() 还是不.Take(),这是个问题

C# .Net 3.5 使用 LINQ 替换文件扩展名的代码

asp.net-mvc - 用于 YUI 控件(雅虎用户界面)的 ASP.NET MVC HtmlHelper 扩展?