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

Mel*_*sus 13 asp.net-mvc html-helper .net-3.5

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

问题是它是一个布尔值(有或没有)

它应该看起来像:

<input name="a" value="" autofocus>
Run Code Online (Sandbox Code Playgroud)

我试过了 :

<%= Html.TextBox( "a", null, new { autofocus } ) %>
Run Code Online (Sandbox Code Playgroud)

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

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

Bra*_*ord 25

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

根据布尔属性HTML5规范:

如果该属性存在,则其值必须是空字符串,或者是属性的规范名称的ASCII不区分大小写匹配的值,没有前导或尾随空格.

所以要么

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

应该是有效的.