为什么jQuery替换只能工作一次?

时间:2017-01-02 21:03:38

标签: jquery html

我有一些jQuery代码,每次提交表单时都会替换div中的数据。出于某种原因,replaceWith函数仅替换div一次?谁能告诉我我错过了什么?

HTML

<div class="count"></div>

的jQuery

$(document).ready(function(){
  $("#like_unlike").submit( function(e) { //Second Form
    e.preventDefault();
    var url = "update_likes.php"; //Grab URL from form
    var formdata = $(this).serialize();
    console.log(formdata);   
    $.post(url, formdata, function(response) {
      //Post the form
      $('.count').replaceWith(response);
    });
  });
}); 

1 个答案:

答案 0 :(得分:6)

replaceWith()不会“替换div中的数据” - 它实际上会替换元素,这意味着您将无法再选择它,因为.count将在您的代码执行后消失。

相反,将.count的内部HTML替换为html(),如下所示:

$('.count').html(response);
相关问题