如何获取mongo db当前正在显示的项的id

时间:2015-12-29 10:07:53

标签: mongodb meteor

我将meteor用于我的项目,我有以下代码:

<div class="col-md-4 activities_text">
        <center><h2>Learn</h2><br/><br/></center>
        {{getContent.description}}
        </div>
        <div class="col-md-8">
        <div class="button_absolute">
        <button class="button_ahead"><span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span></button>
        </div>
        <img class="activities_image" name="image" src="{{getContent.src}}"/>
        </div>
        </div>

我想获取关于我的HTML当前用于标题和描述的图像的“src”的项目的id或(随后是数据库中的全部信息)。所以我可以在点击按钮时交换它们。我试过这个:

 'click .button_ahead':function(event){
      var infoNumber = this.number;
      console.log("this is the number of the image"+this._id+" "+ this.number);
      infoNumber = infoNumber + 1;
      Session.set("number", infoNumber);
    }

但this.number和this._id仍未定义。也许问题是,当我点击按钮时,我应该获得有关图像等的数据..但不是按钮本身。

2 个答案:

答案 0 :(得分:1)

上下文是按钮元素,因此this可能不是您想要使用的内容,如果您真的想使用this尝试使用.call()或{{更改上下文1}}函数。

PS:我想发表评论,但我不能因为代表点而

答案 1 :(得分:0)

$(event.currentTarget)[0].id会在

中显示按钮的ID
'click .button_ahead':function(event){
      var id = $(event.currentTarget)[0].id
      var infoNumber = this.number;
      console.log("this is the number of the image"+id+" "+ this.number);
      infoNumber = infoNumber + 1;
      Session.set("number", infoNumber);
    }

所以在你的模板中你可以简单地把

<button class="button_ahead" id="this._id"><span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span></button>

获取您正在显示的当前资源的ID。

您还可以使用更简单的event.currentTarget.id