jquery - Prevent browser jump to top page when submit the form -


i trying prevent page jumping top when user submits form. there many people suggesting return false. however, prevents form submitted too.

i wondering if me it.

**jquery**  $('#submit').click(function(){ //doesn't work     return false; })  $('form').submit(function(){  //doesn't work either     return false;        })    **html** <form...> <input>... <input>... <input>...  <input type='submit' id='submit' value='submit'>  </form> 

when submit form loading new page. returning false in submit handler tells browser not bubble event , not perform default event, in case submiting form, why nothing happening when this.

there several solutions problem. if redirecting user (after form submission) form page errors, can include "fragment identifier" in url. have fragment identifier point id of form , browser automatically go part of document. example:

<form id="my-form">     ... </form> 

and url be:

host/path?query=querystring#my-form 

if want avoid having page reload, try doing validation in js. jquery validation plugin easy use , fantastic. see here find out how use it. it's great because of heavy lifting , has set of common, default validation methods.

alternatively can submit form via ajax , return validation errors server picks up. if don't have errors can redirect next page. if there errors stick them in form or display alert. solution avoid having replicate complex validation logic on client (if want use jquery validate plugin).


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -