我在使用 ajax 将 HTML 附加到动态生成的
##### in the code is where the main points are
- 我使用php作为后端,我很确定它很好,因为我已经调试过了。似乎它与jquery有关。似乎不允许将 html 附加到 ajax 调用中,但允许它在外部。
谢谢你的帮助,我真的很感激。如有任何误解,请告诉我,我会尽快澄清。
我有一个点击事件,它将向跨度添加 html
$(document).ready(function () {
// Sort default refresh
var selected_skill = $('#order').find(":selected").text();
sortBySkill(selected_skill); ##### Creates the dynamic <span>'s #####
$("#feed").on('click', '.post', showForm); ##### Problem lies within this code, where I am appending html to a <span> #####
});
似乎附加在 ajax 调用中不起作用,但在它之外起作用。有谁知道为什么以及如何解决这个问题?
function showForm(){
// Check if the user is logged in
var username = $('#username').val();
var form_id = "form_comment_" + this.id;
var comment_span = "comment_span_" + this.id;
if ($("#" + form_id).length == 0)
{
//Displays all of the comments already made
$.ajax({
type: "POST",
url: "ajax_calls.php",
data: {fpost_id:this.id, xswitch:"SFC"},
dataType: "json",
error: function (qXHR, textStatus, errorThrown) {
console.log(errorThrown);
},
success: function (result){
// Display comments
console.log(result[0].username);
for (var i = 0; i < result.length; i++){
var comments_html = result[i].username + " : " + result[i].comment + "<br>";
$("#span_post_" + this.id).append(comments_html); // ##### This is not being appended #####
$("#span_post_" + this.id).append("Why does this not append?");
}
}
});
$("#span_post_" + this.id).append("This appends fine"); // ##### THIS APPENDS FINE #####
} else{
$("#" + comment_span).remove(); // Remove the element if already exists
}
return false;
}
本质上,我通过生成动态跨度的技能进行排序(我想在其中使用 ajax 添加数据)
function sortBySkill(selected_skill){
$.ajax({
type: "POST",
url: "ajax_calls.php",
data: {skill:selected_skill, xswitch:"SBS"},
dataType:"json",
error: function (qXHR, textStatus, errorThrown) {
console.log(errorThrown);
},
success: function (result) {
for (var i = 0; i < result.length; i++){ // ##### Dynamically generated span #####
var str_post = "<span id='span_post_" + result[i].idPost + "'>" +
"<a href='' class='post' id='" + result[i].idPost + "'>" +
"Username:" + result[i].username + " | " +
"Steam: " + result[i].steam + " | " +
"Skill Level: " + result[i].skill + "<br>" +
"Post: " + result[i].description + "<br>" +
"Date: " + result[i].date + "<hr> </a> </span>";
$("#feed").append(str_post);
}
}
});
}