此代码会重新加载整个页面,而不是每隔一段时间简单地替换 #form
元素。有人可以提出修复建议,以便只替换 #form
而不会重新加载整个页面吗?
jQuery:
$("#login").submit(function() {
$.post($(this).attr("action"), $(this).serialize(), function(html) {
var htmlString = (new XMLSerializer()).serializeToString(html);
$("#form").replaceWith(htmlString);
$("html, body").animate({
scrollTop: $("#message").offset().top
}, 500);
});
return false;
});
HTML:
<div id="form">
<form id="login" action="submit.php" method="post">
<input type="text" name="name">
<button type="submit">Submit</button>
</form>
</div>
在我看来,replaceWith() 不会消除绑定(bind),但也许会。 . .
最佳答案
It doesn't seem to me that replaceWith() would overwrite the bindings, but perhaps it does . . .
本质上。
您将事件绑定(bind)到 DOM 节点,然后用其他东西(没有绑定(bind))替换该 DOM 节点。
尝试 .live
对于 自动重新绑定(bind) (想不出更好的术语) 与给定选择器匹配的 DOM 节点的事件处理程序,在这样的任何时间点匹配成立。
关于javascript - jQuery & replaceWith() : reloading page every other time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6349936/