[英]jQuery .post() scrolls to top
I have a button <a href="#" onClick="buy(id)">Buy</a>
我有一个按钮
<a href="#" onClick="buy(id)">Buy</a>
The buy
function uses $.post()
function to process the request and return an updated shopping cart bar, and also updates the div with the cart bar that is on the top of the page. buy
函数使用$.post()
函数处理请求并返回更新的购物车栏,并且还使用页面顶部的购物车栏更新div。 And then it scrolls up to top, but I don't want it to scroll up. 然后它向上滚动到顶部,但是我不希望它向上滚动。 How can I fix this?
我怎样才能解决这个问题? The
buy
function returns false
. buy
函数返回false
。
Try to use preventDefault()
尝试使用
preventDefault()
$('#element').click(function(e){
e.preventDefault();
//do whatever
});
you have to pass return false;
您必须通过
return false;
: :
<a href="#" onClick="buy(id); return false;">Buy</a>
another option is to give an id or class name and in the callback stop the default behavior. 另一个选择是提供ID或类名,并在回调中停止默认行为。
<a href="#" id='buyBtn'>Buy</a>
then jQuery event: 然后jQuery事件:
$('#buyBtn').on('click', function(e){
e.preventDefault();
buy(id);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.