【问题标题】: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 变量,并且仍然让用户填写简单模式的其余部分?
【发布时间】:2016-09-11 17:36:01
【问题描述】:

我正在制作一个流星网络应用程序,用户将在其中单击一个 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"}}

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

【问题讨论】:

    标签: javascript html mongodb meteor schema


    【解决方案1】:

    您可以像这样使用 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 是您使用 Autoform 发送的表单的 ID。

    【讨论】:

    • 这似乎是一个很有希望的答案,你能把我链接到一个有文档的网站吗?我不确定我是否完全理解 autoform 钩子是什么。感谢您的回答!
    • 我不知道什么钩子对我的生活有帮助
    • 显示您尝试过的内容。上面的代码允许您在提交之前编辑文档,因此您可以使用您的 js 变量填写您想要的字段(在提交到服务器之前)
    • 如果您想要在向用户显示之前填写表格,您只需将变量分配给模板中输入的“值”字段。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-04
    相关资源
    最近更新 更多