【发布时间】:2011-02-24 01:45:50
【问题描述】:
以下代码旨在执行纯粹的 ajax POST 请求,而不是似乎通过 ajax 执行 POST,然后浏览器导航到响应。
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>
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。但是由于某种原因,浏览器 正在更改页面。
知道我在这里做错了什么吗?谢谢。
【问题讨论】:
标签: javascript jquery html ajax http-post