gpt4 book ai didi

javascript - jQuery click() 不适用于 JSON

转载 作者:行者123 更新时间:2023-11-28 11:27:09 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com