php - Disable the enter key on a jquery-powered form -
i have form using form jquery plug in handel posting of data. in example working data psoted php file reades database , echos result displayed below from.
the code works 1 glitch. if hit enter button while text filed selected cleared including result has been written screen. possible disable enter key , prevent doing this?
form:
<html> <head> </head> <p>enter code here <form name="form" action="" method=""> <label for="name" id="name_label">name</label> <input type="text" name="name" id="name"/> <input type="button" value="get" onclick="get();"/> </form> <div id="age"></div> </p> </body> </html>
script:
function get() { $.post('data.php', {name: form.name.value}, function(output) { $('#age').hide().html(output).fadein(1000); }); } }
cheers.
you should consider using jquery forms plugin. save doing of dirty work, additionally intercept ways of submitting form - instead of having disable return key submit form via ajax.
if don't want that, rid of button onclick event , replace submit button , register function onsubmit handöer:
$('form[name=form]').submit(function(e) { e.preventdefault(); $.post('data.php', {name: form.name.value}, function(output) { $('#age').hide().html(output).fadein(1000); }); } });
Comments
Post a Comment