通过JavaScript附加的HTML不会触发jQuery on('click')功能

时间:2018-12-21 20:07:14

标签: javascript jquery ajax

基本上,当我使用AJAX请求创建评论,然后将评论div结构附加到我的评论容器div时,用于触发删除该评论的AJAX请求的X图标不起作用,我不会没有任何错误。

如果创建注释然后刷新页面,则可以将其删除,因此问题仅与使用JavaScript编程创建的注释有关。

我不确定如何解决问题,因为控制台没有任何错误。

这是用于发布评论的AJAX:

java

这是用于删除评论的AJAX:

public class LevelLoader : MonoBehaviour
{
public static LevelLoader instance;
public GameObject screen;
public Slider slider;
public TextMeshProUGUI progressText;
private float oldProgress = 0f;

private void Awake()
{
    DontDestroyOnLoad(gameObject);
    DontDestroyOnLoad(screen.transform.parent.gameObject);

    instance = this;
    screen.SetActive(false);
}

public void LoadLevel(int sceneIndex)
{
    StartCoroutine(LoadAsync(sceneIndex));
}

IEnumerator LoadAsync(int sceneIndex)
{
    AsyncOperation op = SceneManager.LoadSceneAsync(sceneIndex);
    screen.SetActive(true);
    while (!op.isDone)
    {
        System.Random rand = new System.Random();
        float progress = Mathf.Lerp(oldProgress, Mathf.Clamp01(op.progress / .9f), rand.Next()); 
        // for smoothness (doesn't actually work i think)

        slider.value = progress;
        progressText.text = (progress * 100f).ToString("f0") + "%";
        oldProgress = progress;

        yield return null;
    }
    screen.SetActive(false);
}

最后,这是我使用JavaScript创建的HTML结构。它使用刀片模板和PHP:

$('.post-comment').on('click', function(event) {
    event.preventDefault();
    var userId = $("input[name=user_id]").val();
    var imageId = $("input[name=image_id]").val();
    var comment = $("textarea[name=comment]").val();

    $.ajax({
        method: 'POST',
        url: urlComment,
        data: {
            userId: userId,
            imageId: imageId,
            comment: comment,
            _token: token
        }
    }).done(function(response) {
        var commentsCount = response.image.comments;
        var comment = response.comment.comment;

        var appendHtml =    '<div class="comment-flexbox">' +
                                '<div class="comment-container">' +
                                    '<a href="../profile/' + response.image.user.username + '">' +
                                        '<img class="comment-picture" src="../storage/uploads/profile_pictures/edited/'+ response.image.user.image_file_name +'">' +
                                    '</a>' +
                                '</div>' +
                                '<div class="comment-info-container">' +
                                    '<a href="../profile/'+ response.image.user.username +'">' + response.image.user.username + '</a>' +
                                    '<p>' + comment + '</p>' +
                                '</div>' +
                                '<div class="comment-actions-container">' +
                                    '<i class="fas fa-times delete-comment" data-id="' + response.comment.id + '"></i>' +
                                '</div>' +
                            '</div>';

        $("textarea[name=comment]").val("");

        if ($('.comments-container').length) {
            $('.comments-container').prepend(appendHtml);
        } else {
            var x = document.createElement("div");
            var artworkInfoContainer = $('.artwork-info-container');
            artworkInfoContainer.append(x);
            x.className = "comments-container";
            $('.comments-container').prepend(appendHtml);
        }

        $('.comments-count').html("<i class='far fa-comments fa-fw'></i>" + commentsCount + " Comments")
    })
});

1 个答案:

答案 0 :(得分:2)

在用于删除评论的代码中,您应该使用文档来委托事件,如下所示:

<cfquery name="splitname" datasource="dsn">
    SELECT empID, fullname 
    FROM dbo.employees
</cfquery>

<cfloop query="splitname">
    <cfquery datasource="dsn">
        UPDATE dbo.employees
        SET
            lastname = <cfqueryparam value="#trim(listFirst(splitname.fullname, ","))#" cfsqltype="varchar">,
            firstname = <cfqueryparam value="#trim(listlast(splitname.fullname, ","))#" cfsqltype="varchar">
        WHERE empID = <cfqueryparam value="#splitname.empID#" cfsqltype="varchar">
    </cfquery>
</cfloop>

这样,文档中附加的所有新元素仍将“听到”该事件。