jQuery click()不适用于JSON

Gre*_*nih 0 javascript jquery json click

我有一个从MySQL数据库中获取的JSON生成数据列表.我现在要做的是,当点击列表中的任何项目时,它将被添加到我的表单的隐藏输入中,但问题是,如果我这样做:

$(".buttonZvrst").click(function(){
alert("this is a test");
});
Run Code Online (Sandbox Code Playgroud)

什么都不会发生.如果我选择不在JSON生成列表中的任何其他元素,它将起作用.它不起作用,因为它是在以后产生的吗?我需要帮助!这是我的getZvrsti函数,其中JSON是.

function getZvrsti(id) {
            // Save the request to our requests object
            request[id] = $.getJSON('test.php?parent='+id, function(data) {
                var html = "";
                $.each(data, function(id, name) {
                    if(name['id'] in izbrani){
                        if(izbrani[name['id']] == true){
                            html += '<li id="drugaZvrst" class="izbran"><a class="buttonZvrst" href="#" id="'+name['id']+'">'+name['name']+'</a></li>';
                        }
                        else{
                            html += '<li id="drugaZvrst"><a class="buttonZvrst" href="#" id="'+name['id']+'">'+name['name']+'</a></li>';
                        }
                    }
                    else
                    {
                        izbrani[name['id']] = false
                        html += '<li id="drugaZvrst"><a class="buttonZvrst" href="#" id="'+name['id']+'">'+name['name']+'</a></li>';
                    }


                });
                // Append the list items and then fade in
                listUl.append(html);
                druga.show(400);
                // We no longer have a request going, so let's delete it
                request = false;
            });
        }
Run Code Online (Sandbox Code Playgroud)

Mar*_*lde 8

您正在使用.click在执行脚本期间将其绑定一次以获取可用元素.对于动态元素,您需要在jQuery Docs上.live查找.live():

.live:为现在和将来与当前选择器匹配的所有元素附加事件的处理程序.