Autofocus on EditorFor

前端 未结 4 1291
梦谈多话
梦谈多话 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:34

    I've been following this thread and I may have stumbled on an answer to your question about autofocus on EditorFor - this is all Asp.Net 4.5 and MVC 5, not that it matters.

    1. In the Scripts folder I have a jQuery script file:

      $(function(){ $('.someclassname').focus(); });

    I add the script name to the BundleConfig and render it in the view.

    1. In the view I add the classname to the EditorFor
    2. I then add the type="text" autofocus="autofocus" to the EditorFor's @class. So, new{@class="form-control", type="text", autofocus="autofocus"
    3. That's pretty much it, when the DOM loads the .someclassname field gets the cursor focus... PS. In fact if you just do (3) it works also...

提交回复
热议问题