我怎么能让流星简单模式中的一个字段等于一个js变量,并且仍然让用户填写简单模式的其余部分

How can I make a field in a meteor Simple Schema equal to a js variable and still let the user fill out the rest of the simple schema?

本文关键字:模式 简单 用户 变量 余部 流星 怎么能 于一个 字段 一个 js      更新时间:2023-09-26

我正在制作一个流星网络应用程序,用户可以点击html按钮。单击此按钮后,用户需要被引导到另一个页面,其中包含由流星简单模式包生成的一些表单。简单模式中的第一个字段需要自动给定一个字符串值"hello",然后用户将使用页面上的输入字段填写简单模式中其余的字段。我不确定的是如何将第一个值自动设置为这个字符串值。以下是我的一些代码:

简单的模式声明:

LobbySchema = new SimpleSchema({
  game: {
    type: String,
    label: "Game"
  },
  console: {
    type: String,
    label: "Console"
  },
  players: {
    type: Number,
    label: "Players"
  },
  mic: {
    type: Boolean,
    label: "Mic"
  },
  note: {
    type: String,
    label: "Note"
  },
  gamertag: {
    type: String,
    label: "Gamertag"
  },
  createdAt: {
    type: Date,
    label: "Created At",
    autoValue: function(){
      return new Date()
    },
    autoform: {
      type: "hidden"
    }
  }
}); 

当点击html按钮时,模式"game"中的第一个字段需要被赋予值"hello"。现在,我可以通过onclick函数将该值分配给一个javascript变量:

function getElementText(elementID){
   var elementText = "hello";
}

该按钮将调用getElementText函数,并使elementText变量等于"hello"。现在,我需要将简单模式中的第一个字段分配给这个变量值"hello",然后拥有它,这样用户现在就可以用输入字段填充模式的其余部分,并用以下代码自动生成到html中:

{{> quickForm collection="Lobby" id="insertLobbyForm" type="insert" class="newLobbyForm"}}

如果你不想提供答案(也许它碰巧比我想象的更复杂),那么我会很高兴收到一个网站的链接,这可能会帮助我。如果我没有很好地解释上面的情况,我也非常愿意解释关于这个问题的任何事情。

您可以像这样使用AutoForm挂钩:

AutoForm.hooks({
    app_create: {
        before: {
            method: function (doc) {
                // Do whatever assignment you need to do here, 
                // like doc['game'] = "hello";  //then 
                return doc;
            }
        },
        onSuccess: function (formType, result) {
        },
        onError: function (formType, error) {
        }
    }
});

此处app_create是您使用自动表单发送的表单的id。