玉 - 为什么只有循环中的第一个按钮正在工作

问题描述:

在玉视图模板中,有一个循环创建的按钮。该按钮调用一个函数(在公共.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 
+0

:要解决这个问题是使用一类,而不是ID

的一种方式,是这样的:

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(data-id="#{game.game.id}") Play 

而在你的JS,在类中添加事件侦听器看看,你可以格式化该帖子吗? – xShirase

+0

对不起,第一篇文章和我在iPhone上(在途中) – voltaire

+0

没问题,现在好多了,至少我们可以看到你在说什么:) – xShirase

您正在循环中创建具有相同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 

}); 
+0

谢谢(我们该怎么避免这种情况?) – voltaire

+0

避免什么?我提供了一个解决方案的想法 – xShirase

+0

;)在stackoberflow的建议,它说避免评论与“谢谢”。我一定会尝试你的解决方案。如何在你的onclick事件函数(你的//做任何你想做的事情)的情况下如何在没有另外点击的情况下调用我的函数(在.js文件中)?对不起,我用不写在客户端的功能。 – voltaire