Autofocus on EditorFor

前端 未结 4 1160
梦谈多话
梦谈多话 2021-01-11 22:51

I would like to autofocus on an editorfor in my application, but I can\'t seem to do that. I have successfully used autofocus on a textbox, but I would like to use an editor

4条回答
  •  星月不相逢
    2021-01-11 23:37

    This s because you are using EditorFor instead of something specific like TextBoxFor.

    @Html.TextBoxFor(model => model.Name, new { htmlAttributes = new { 
                        @class = "form-control" }, autofocus="autofocus"})
    

    Or you can do that using jQuery:

    @Html.EditorFor(model => model.Description) @Html.ValidationMessageFor(model => model.Description)
    $(function() { $('.focus :input').focus(); });

    Update:
    As you know TextBoxFor always creates a textbox with type input, But EditorFor is a little bit smart, it renders markup based on the datatype of the property.

提交回复
热议问题