javascript - jQuery click() 不适用于 JSON

标签 javascript jquery json click

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

$(".buttonZvrst").click(function(){
alert("this is a test");
});

什么都不会发生。如果我选择 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;
            });
        }

最佳答案

您正在使用.click,它在脚本执行期间为可用元素绑定(bind)一次。对于动态元素,您需要 .live 查找 .live()在 jQuery 文档上:

.live : Attach a handler to the event for all elements which match the current selector, now and in the future.

关于javascript - jQuery click() 不适用于 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7895358/

相关文章:

javascript - React 组件的 <svg> 元素字符串

javascript - Jquery隐藏和显示按钮

javascript - 平均堆栈 : "SyntaxError: Unexpected token f" Comments not posting

jquery - Blueimp jQuery 文件上传和 HttpHandler : No File and always "OPTIONS" HttpMethod

javascript - 如何在 jQuery 中解析 JSON 数组,可视化页面上的每个元素,并在每个要分割的元素上提供一个选项?

json - Kendo 网格未绑定(bind)到我的 WCF 服务

javascript - 如何使用适用于 iOS 的 phonegap 插件将数组作为参数传递给函数

javascript - 如果我对 'Cannot read property ' *** *' of undefined' 没问题怎么办?

javascript - 延迟设置 Whois 脚本

javascript - 如何在 HTML 中添加 JQuery 代码?