2017-10-07 51 views
-2

我試圖創建一個應用程序,它在輸入字段中輸入後保存信息!我在添加具有多個輸入字段的表單時遇到了一些問題。如何在Meteor.js中添加所有表單的元素?

這是我的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> 

請告訴我,我怎麼可能增加超過2個輸入,並通過點擊提交按鈕顯示呢?

+0

很難理解什麼是你的問題。但是,您是否嘗試重複使用替換name =「text」並替換屬性名稱並相應修改數據引用? – AirBorne04

回答

1

這真的不清楚你的問題是什麼。這可能就像包含第二個文本字段一樣簡單,並將其與第一個字段同時保存到mongo中?

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:

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

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

您的代碼工作不到風度( 在Object.click .subm(body.js無法讀取的不確定 屬性 '值':20) -const文本= target.text.value; - 這條( –

+0

抱歉。 這是我的錯誤 你的代碼工作謝謝) 更改答案PLS-爲:! 常量目標= event.target; 常量文本= target.text.value; 常量文本2 = target.text2.value; ({text,text2,createdAt:new Date()}); target.text.value =''; target.text2.value =''; –

相關問題