简体   繁体   English

如何在 Meteor.js 中提交时添加所有表单元素?

[英]How to add all form's elements on submit in Meteor.js?

I'm trying to create an app that saves information after I enter it in input fields!我正在尝试创建一个在输入字段中输入信息后保存信息的应用程序! I'm having some problems with adding my form that has more than 1 input field.我在添加具有 1 个以上输入字段的表单时遇到了一些问题。

These are my HTML and JS files:这些是我的 HTML 和 JS 文件:

import { Template } from 'meteor/templating';

import { Tasks } from '../api/tasks.js';

import './body.html';

Template.body.helpers({
  tasks() {
    // Show newest tasks at the top
    return Tasks.find({}, { sort: { createdAt: -1 } });
  },
});

Template.body.events({
  'submit .new-task'(event) {
    // Prevent default browser form submit
    event.preventDefault();

    // Get value from form element
    const target = event.target;
    const text = target.text.value;

    // Insert a task into the collection
    Tasks.insert({
      text,
      createdAt: new Date(), // current time
    });

  },
});
<body>
  <div class="container">
    <header>

      <form class="new-task">
        <input type="text" name="text" placeholder="Type to add new tasks" />
      </form>
    </header>

    <ul>
      {{#each tasks}}
        {{> task}}
      {{/each}}
    </ul>
  </div>
</body>

<template name="task">
  <li>{{text}}</li>
</template>

Please tell me how could I add more than 2 inputs, and display it by clicking submit-button?请告诉我如何添加 2 个以上的输入,并通过单击提交按钮显示它?

It's really unclear what your problem is.真的不清楚你的问题是什么。 Could this be as simple as just including a second text field and saving it to mongo at the same time as the first field?这可能就像包含第二个文本字段并将其与第一个字段同时保存到 mongo 一样简单吗?

html: html:

<form class="new-task">
  <input type="text" name="text" placeholder="Type to add new tasks" />
  <input type="text" name="text2" placeholder="Type to add something else" />
</form>

js: js:

const target = event.target;
const text = target.text.value;
const text2 = target.text2.value;

Tasks.insert({ text, text2, createdAt: new Date() });

我建议看看这个用于 Meteor 表单管理的很棒的包: https : //github.com/aldeed/meteor-autoform

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM