简体   繁体   English

jQuery .click()事件不起作用

[英]jquery .click() event not working

This is my jquery code: 这是我的jQuery代码:

$(function(){
$.post("/person/keywords/get/", function(data){
    for(i=0; i<data.length; i++)
    {
        count = count + 1;
        $(".keywords-set").append('<div class="keyword-item"><span class="keyword" id="keyword-'+count+'">'+data[i]+'</span><textarea class="comment" id="comment-'+count+'"></textarea></div>');
    }
});

$(".keyword-sets").on('click', "[id^=keyword]", function(event) {
    event.preventDefault();
    alert("yes");
});
})

On page load, I fetch some datas from the server then append it to the html. 在页面加载时,我从服务器获取了一些数据,然后将其附加到html。 The click event is not working for that appended html items. click事件不适用于该附加的html项。 I'm getting this problem for all the items which I got from the server by .post() request. 对于通过.post()请求从服务器获取的所有项目,我都遇到了此问题。

Here is my complete and HTML and jQuery code: 这是我完整的HTML和jQuery代码:

https://gist.github.com/3023937 https://gist.github.com/3023937

One more thing, I think the problem occurs bcoz of using the jQuery code inside the html(not as an external file). 还有一件事,我认为问题出在使用html(而不是外部文件)内部的jQuery代码时。 I'm having this similar type of code in another project where I'm having jquery code in external file and it works without any problem. 我在另一个项目中有类似类型的代码,在我的外部文件中有jquery代码,它的工作没有任何问题。 Why is it so? 为什么会这样呢?

PS If you need any more details, I can give you. PS:如果您需要更多详细信息,我可以给您。

Thanks! 谢谢!

This code: 这段代码:

$(".keyword-sets")

says "sets" plural, but I don't see that symbol anywhere else. 说“集合”复数,但在其他任何地方都看不到该符号。 Perhaps you meant: 也许您的意思是:

$(".keyword-set").on("click", ...

edit — oops ".keywords-set" ... 编辑 —糟糕“ .keywords-set” ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM