以下代码旨在执行纯 ajax POST 请求,但实际上它似乎是通过 ajax 进行 POST,然后浏览器会导航到响应页面。
HTML...
据我所了解,
你有什么想法吗?谢谢。
HTML...
<div id="bin">
<form class="add" method="post" action="/bin/add/">
<p>I'm interested! Save for later.</p>
<input type="hidden" name="product_id" value="23423">
<input type="submit" value="Save">
</form>
<form style="display:none;" class="remove" method="post" action="/bin/remove/">
<p>I changed my mind--I'm not interested.</p>
<input type="hidden" name="product_id" value="23423">
<input type="submit" value="Unsave">
</form>
</div>
The jQuery...
$('#bin form').submit(function() {
$.post($(this).attr('action'),{
success: function(data) { $(this).hide().siblings('form').show() },
data: $(this).serialize()
});
return false;
})
据我所了解,
return false;
这一行代码应该意味着无论如何,任何对提交函数的调用、单击“提交”按钮或按下回车键都意味着我的函数将执行,并且浏览器不会导航到/bin/add
或/bin/remove
。但出于某种原因,浏览器仍然在更改页面。你有什么想法吗?谢谢。
event.preventDefault()
,我现在能够在 Firebug 中检查错误了。谢谢! - Chris W.