开发者

What's the MVC3 Razor equivalent of Page.Form.DefaultFocus?

开发者 https://www.devze.com 2023-04-11 05:28 出处:网络
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?

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
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消