【问题标题】:Selected option tag using Meteor使用 Meteor 选择的选项标签
【发布时间】:2016-03-24 21:12:21
【问题描述】:

我有一个带有 boxId 字段的 Items 集合(和一个 Boxes 集合),我希望能够通过 select 标记 更改项目的 boxId。

这是我的模板:

这就是我定义 boxOptions 助手的方式:

我如何获取一个项目的 boxId 并使用它找到合适的 option 标签,然后赋予它 selected 属性?

【问题讨论】:

    标签: meteor


    【解决方案1】:

    创建一个事件

    Template.item.helpers({
      "change select": function(event){
        const boxId = event.target.value;
        items.update({_id: this._id}, {$set: {boxId: boxId}});
      }
    })
    

    请注意,这假设您使用的是包 insecure 和 autopublish。如果您不使用这些并且确实不应该使用,那么您最好阅读以下内容:

    • 参数验证
    • 出版物
    • 流星方法

    【讨论】:

    • 我已经能够更新该字段。问题是选定的选项标签始终是第一个呈现的。所以我认为我需要在正确的选项标签上放置一个“选定”属性。那是我的问题
    【解决方案2】:

    使用Template.parentData() 可以访问项目的ID。这是帮手:

    selected: function () {
      if (this._id == Template.parentData().boxId) {
        return "selected";
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-04-08
      • 1970-01-01
      • 2017-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-21
      相关资源
      最近更新 更多