1

I am populating a drop down menu with different items in a Meteor Collection Cases.

  <select id="caseChoice">
   {{#each case}}
     <option>{{name}}</option>
   {{/each}}
  </select>

I get the value of the selection, aka the name by:

 var caseName = document.getElementById("caseChoice").value;

And I can retrieve the id of the case by using:

 var caseID = Cases.findOne({name:caseName})._id;

However I am planning on letting users create cases with the same name. Is their a way to pass the id through the {{each}} without displaying the _id in the drop down selection?

4

1 回答 1

2

如果您的意思是希望每个 html 标记都包含 id,您可以使用选项的 value 属性,或者使用 html5 数据属性:

{{#each case}}
  <option value="{{_id}}" data-id="{{_id}}">{{name}}</option> // can use value= or data-id=
{{/each}}

这将在浏览器的源代码中可见,但不会在用户屏幕上显示。或者,如果您想通过模板事件访问 id,它也将在那里可用:

"click option" /*or whatever event handler is appropriate here*/: function(event, template){
  var id = template.data._id;
  /* alternatively you can call this._id which will generally give you the same result */
  /* then do your event handling, etc. */ 
}
于 2014-05-01T16:29:22.097 回答