javascript - jQuery & replaceWith() : reloading page every other time

标签 javascript jquery

此代码会重新加载整个页面,而不是每隔一段时间简单地替换 #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/

相关文章:

javascript - 更改 JavaScript 中的变量引用

javascript - Jquery、PHP和Ajax形式无需刷新

javascript - 旋转木马幻灯片和导航栏更改之间的 Bootstrap 延迟

javascript - 如何获取 Firebase 数据库集合中文档的唯一 ID(键)?

javascript - 更新云功能中的firestore

JavaScript - 添加 ID 和类

javascript - 从 jquery 数据表中选择一个值

jquery - 删除不起作用方法不允许 (#405) gridview yii2

javascript - IE 7 中的 javascript NaN 是什么原因导致的?

javascript - 当用作对象键时,为什么 JavaScript 会将一个字符串数组转换为字符串?