If I wanted 开发者_如何学编程to conditionally set which field should have focus using ASP.NET WebForms, I would set Page.Form.DefaultFocus. What is the MVC3 Razor way to do the same?
Background: I have a login page. Sometimes the username field is automatically filled in, sometimes it's blank. If the username field is blank, I want it to have the focus. If the username field is filled in, I want focus on the password field.
How about using jQuery:
$(function() {
var username = $('#Username'); // TODO: adjust selector if necessary
if (username.val() === '') {
username.focus();
} else {
$('#Password').focus(); // TODO: adjust selector if necessary
}
});
精彩评论