javascript - Submit form when enter is pressed using jQuery submit() -


i have handler catch event of "enter" inside set of inputs in context of authentication function, it's not working when press key. code follows :

$('#login form').submit(function(e) { login(e); });  function login(e) {     e.preventdefault();      var l = $('#logo a').attr('href');     var name = $('#login input[name="login_username"]').val();     var pass = $('#login input[name="login_password"]').val();      $.ajax({         type: 'post',         url: l+'user/login',         data: 'username='+name+'&password='+pass,         cache: false,         datatype: 'json',         success: function(response)         {             if (response.status == true)                 window.location = response.redirect;             else                 jquery.facebox('<p class="facebox-notice">'+response.error+'</p>');         }     }); } 

it work, however, button have same purpose :

$('#dologin').click(function(e) { login(e); }); 

any ideas why? cheers!

edit

the markup follows :

<div id="login">   <form action="" method="post">     <input type="text" name="login_username" />     <input type="password" name="login_password" />     <a href="" id="dologin">login</a>   </form> </div> 

so goal submit form when enter pressed, correct?

the easiest way achieve use submit button

<input type="submit" value="login" /> 

instead of link. event handled browser.

demo

if prefer have link because of style, can use css style button , make link.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -