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
Post a Comment