我有一些jQuery代码,每次提交表单时都会替换div中的数据.由于某种原因,该replaceWith功能只更换div一次?谁能告诉我我错过了什么?
HTML
<div class="count"></div>
Run Code Online (Sandbox Code Playgroud)
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);
});
});
});
Run Code Online (Sandbox Code Playgroud)
replaceWith()不会"替换div中的数据" - 它实际上替换了元素,这意味着您将无法再选择它,因为.count在代码执行后它将会消失.
相反,替换.countwith 的内部HTML,html()如下所示:
$('.count').html(response);
Run Code Online (Sandbox Code Playgroud)