玉 - 为什么只有循环中的第一个按钮正在工作
问题描述:
在玉视图模板中,有一个循环创建的按钮。该按钮调用一个函数(在公共.js库中)并发送参数。 在循环中有n个项目,n个按钮,但当第一个按钮被点击时只有一个动作。玉 - 为什么只有循环中的第一个按钮正在工作
each game in games
td= moment(game.game.date).format('YYYY-MM-DD h:mm:ss a')
if (game.game.users.white==user.key)
td= game.game.users.black
else
td= game.game.users.white
td= game.game.id
td
input#username(type="hidden", value="#{user.key}", name="username")
input#rgame(type="hidden", value="#{game.game.id}", name="rgame")
button#resumegame Play
答
您正在循环中创建具有相同ID #resumegame的按钮。 id代表一个独特的元素,因此您点击时附加的事件只会在第一个按钮#resumegame上运行。这是痛苦的
$('.resumegame').on('click',function(){
var gameId = $(this).data('id');
// Do whatever you want here,
// you know the id of the button that has been clicked via the data-id attribute
});
:要解决这个问题是使用一类,而不是ID
的一种方式,是这样的:
而在你的JS,在类中添加事件侦听器看看,你可以格式化该帖子吗? – xShirase
对不起,第一篇文章和我在iPhone上(在途中) – voltaire
没问题,现在好多了,至少我们可以看到你在说什么:) – xShirase