使用 js 插入 html



我在提交表单后尝试使用 ajax 以获得另一个

$(document).ready(function() 
{ $("#my_form").submit(function(event) 
{ event.preventDefault(); 
$this = $(this); 
$.ajax({ 
type: "POST",
data: $this.serialize(), 
success: function(data) 
{ console.log(data); 
document.getElementById("my_form").replaceWith(data.form1);
}, 
error: function(data) 
{ console.log(data); 
} 
}); 
}); 
});

但是在页面上,HTML代码作为字符串插入,如何解决此问题?

看起来你正在替换你的subbmitting表单,你可以使用jQuery .html((函数来替换。

$(document).ready(function() 
{ $("#my_form").submit(function(event) 
{ event.preventDefault(); 
$this = $(this); 
$.ajax({ 
type: "POST",
data: $this.serialize(), 
success: function(data) 
{ console.log(data); 
//document.getElementById("my_form").replaceWith(data.form1); //remove this.
//add below code
var parent=$("#my_form").parent();
parent.html(data.form1);
}, 
error: function(data) 
{ console.log(data); 
} 
}); 
}); 
});