gpt4 book ai didi

javascript - 使用 jquery 附加按钮获取参数

转载 作者:行者123 更新时间:2023-11-28 15:54:34 24 4
gpt4 key购买 nike

我有一个按钮监听器,如下所示:

$('body').on('click', '.showrecent', function() {
$('#display').toggle();
});

我有一个 json 搜索(我在其中附加一个按钮):

var search = 0;
$.getJSON('data.json', function(data) {
if ((this.Name.toLowerCase()).indexOf(sr) > -1) {
id++;

//blah blah blah

var recent = "<button class = 'showrecent' onclick = 'showrecent(id)'>Recent Transaction</button>";

$('#founded').append(recent);

});
});

基本上,我想用 showrecent 函数传递 id!非常感谢任何帮助!

最佳答案

如果您希望每个函数调用中都有自己的 id,则需要连接。

var recent = "<button class = 'showrecent' onclick = 'showrecent(" + id + ")'>Recent Transaction</button>";

另一种方法是使用 jQuery 绑定(bind)处理程序。

id++;

var thisid = id;
$("<button>", {className:'showrecent',
text:" Recent Transaction",
click: function() {
showrecent(thisid);
}
}).appendTo('#founded');

关于javascript - 使用 jquery 附加按钮获取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19370888/

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