开发者

Enter Key for Login Form

开发者 https://www.devze.com 2023-01-02 13:49 出处:网络
I have a search form and a login form on my website. When the enter button is pressed when the login form has focus, the search runs instead of the login. Is there a way to fix this?

I have a search form and a login form on my website. When the enter button is pressed when the login form has focus, the search runs instead of the login. Is there a way to fix this?

I've already tried using a panel around the login form and use defaultbutto开发者_如何学Pythonn, but the loginview errors when I do this.


You could try setting up a keypress event on your login form. Off the top of my head, something like

$('#loginForm').keypress(function (e) {
  if(e.keyCode=='13') //Keycode for "Return"
     $('#login').click();
  }
});

should work, assuming you give appropriate IDs to the elements involved.


If you're talking about HTML, then this suggests tab order (learn more about that here) is relevant or the order in which the forms were created, the behavior depending on the user agent.


could you try adding attributes at run time like this

Login loginControl = (Login)lvLoginView.FindControl("logLogin"); 
TextBox tbUserName = (TextBox)loginControl.FindControl("UserName");
TextBox tbPassword = (TextBox)loginControl.FindControl("Password");
Button loginButton = (Button)loginControl.FindControl("LoginButton");  
tbUserName.Attributes["onKeyPress"] = "KeyDownHandler('" + loginButton.ClientID + "')";
tbPassword.Attributes["onKeyPress"] = "KeyDownHandler('" + loginButton.ClientID + "')";

and some JS:

function KeyDownHandler(btn){  
// process only the Enter key  
if (event.keyCode == 13)  {    
// cancel the default submit  
  event.returnValue = false;  
  event.cancel = true;   
  var obj = document.getElementById(btn);   
  obj.click();  
}}

UPDATE

Auto-converted to VB.NET courtesy of telerik

Dim loginControl As Login = DirectCast(lvLoginView.FindControl("logLogin"), Login)
Dim tbUserName As TextBox = DirectCast(loginControl.FindControl("UserName"), TextBox)
Dim tbPassword As TextBox = DirectCast(loginControl.FindControl("Password"), TextBox)
Dim loginButton As Button = DirectCast(loginControl.FindControl("LoginButton"), Button)
tbUserName.Attributes("onKeyPress") = "KeyDownHandler('" + loginButton.ClientID + "')"
tbPassword.Attributes("onKeyPress") = "KeyDownHandler('" + loginButton.ClientID + "')"
0

精彩评论

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