2017-02-13 50 views
0
<div class="form-group"> 
    <label>Faculty Name *</label> 
    @Html.TextBoxFor(i => i.FacultyName, new { @class = "form-control", @placeholder = "Technician e.t.c", @name = "txtfacultyname" }) 
    @Html.ValidationMessageFor(i => i.FacultyName, "", new { @class = "error" }) 
</div> 
<div class="form-group"> 
    <label>EmailAddress *</label> 
    @Html.TextBoxFor(i => i.EmailAddress, new { @class = "form-control", @placeholder = "[email protected]", @name = "txtemailaddress" }) 
    @Html.ValidationMessageFor(i => i.EmailAddress, "", new { @class = "error" }) 
</div> 
<div class="form-group"> 
    <label>User Name *</label> 
    @Html.TextBoxFor(i => i.UserName, new { @class = "form-control", @placeholder = "abc e.t.c", @name = "txtusername" }) 
    @Html.ValidationMessageFor(i => i.UserName, "", new { @class = "error" }) 
</div> 
+0

參見[設置焦點上的EditorFor而不使用JavaScript](http://stackoverflow.com/questions/24934846/set-focus-on-an-editorfor-without-使用的JavaScript/24935030#24935030) –

+0

作爲一個方面說明刪除你毫無意義的'new {@name =「xxx」}'幸運的是它什麼也沒做(看你生成的html),因爲它確實,模型綁定會失敗。 –

回答

0

您可以使用$('#FacultyName').focus()

我改變你的代碼有點只是告訴你,它的工作原理。

$(document).ready(function() { 
 
    $('#FacultyName').focus() // This code will only run when the page is done loading/ready 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="form-group"> 
 
    <label>Faculty Name *</label> 
 
    <input id="FacultyName"></input> 
 
</div> 
 
<div class="form-group"> 
 
    <label>EmailAddress *</label> 
 
    <input id="EmailAddress"></input> 
 
</div> 
 
<div class="form-group"> 
 
    <label>User Name *</label> 
 
    <input id="UserName"></input> 
 
</div>

相關問題