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.
if prefer have link because of style, can use css style button , make link.
Comments
Post a Comment