javascript - jQuery .submit() with validation and without page refresh -
javascript - jQuery .submit() with validation and without page refresh -
i have problem others seem have, cannot recommended solution (i.e., "return false;") work. help great!
description:
when form submitted, want validate input in right format (i.e., type="email") , launch alert (i.e., "form submitted.") without page refreshing. currently, alert not appear , page refreshes.
test code:
<!doctype html> <html lang="en"> <head> <!-- javascript --> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> </head> <body> <!-- form --> <form> <input type="email" placeholder="email" value="" size="25px" required="required" id="useremail"> <button type="submit" id="submit">submit</button> </form> <!-- alert on submission --> <script> console.log("ready go!"); $('#submit').submit(function () { alert("form submitted."); homecoming false; }); </script> </body> </html>
you want grab submit
event of form. there no submit
event on button.
$('form').submit(function () { if (*everything ok*) { alert("form submitted."); } else { homecoming false; } });
ideally help identify <form>
, either id or class, i.e.:
<form id="xyz-form">
and alter selector to:
$('#xyz-form').submit(...);
now stop form submitting when there errors. when return false;
isn't path submit callback takes, page is going refresh. if want submit info server without refresh, need approach differently.
javascript jquery html validation form-submit
Comments
Post a Comment