javascript - How to stay on same position after submitting an html form? -


i have simple html form:

<form action="addtocart" method="post"><br><br>       <input name="productid" value="${product.id}" type="hidden"> <input class="submit" onclick="addedcart()" value="<fmt:message key='addtocart'/>" type="submit">    </form> 

every time click on "submit", brings me straight top creates poor user experience because user have scroll down browsing products... can without using script?

-every time click on "submit", brings me straight top.

yes default functionality when submitting forms, repaints dom causes jump , page's top position rendered.

-which creates poor user experience because user have scroll down browsing products

to make user experience can use ajax functionality, tagged jquery in question can try jquery ajax:

$('form').submit(function(e){     e.preventdefault(); // stops form submission      $.ajax({       url:$(this).attr('action'), // action attribute of form send values       type:$(this).attr('method'), // method used in form       data:$(this).serialize(), // data sent php       datatype:"text",       success:function(data){           alert(data); // can alert success message.       },       error:function(err){           console.log(err);       }     });  }); 

i have used datatype:"text", assuming if going echo "added in cart."; kind of message php.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -