在从ajax调用添加数据后调用ID时出现问题

时间:2014-06-30 23:15:51

标签: javascript jquery ajax

确定,

所以我有一个.on click事件,调用ajaxed脚本。该脚本返回一组HTML信息。

现在,我似乎遇到了一个问题,也许我只是没有为此输入正确的搜索条款,所以我很抱歉,如果已经回答了这个问题。

当html被附加到正确的框时,用户可以单击该框上的链接,下拉框并查看新的输入框。用户应该能够键入文本并按Enter键,它应该可以工作。

当我运行一个inspect元素时,我确实看到了元素的ID被正确插入,但它就像javascript甚至无法识别新的元素id。

更新:我真的不确定为什么你需要代码才能回答这个问题,不过现在就是这样。代码正常工作,除非上述情况发生,所以是的我正在追加它,是的我确定我要附加它,请确保在发表评论之前阅读所有内容,谢谢!

 $('body').on('keypress', '.peapComment', function(event) {         
            if(event.which == 13) {
                var peap_id = $(this).attr('rel');
                var comment = $(this).val();
                $.ajax({
                    type: "POST",
                    url: "index.php",
                    data: {
                        page: "postPeapComment",
                        peap_id: peap_id,
                        comment: comment,
                    }
                }).done(function(msg) {
                    console.log(msg);
                    $('#comment_' + peap_id).append(msg);


                });
                console.log('Send to Peap #' + peap_id + ': ' + comment);
                $('#commentbox_' + peap_id).val('');
            }
        });

1 个答案:

答案 0 :(得分:0)

我已经测试了你的代码,它对我来说似乎没有问题。我的假设是从你的ajax调用返回的数据未能返回正确的“Peap_ID”或者没有返回任何东西,因为这是我可以重现你声称你的错误的唯一方法,否则代码你张贴看起来很棒。