繁体   English   中英

如何在Meteor的同一收集字段中添加来自相同表单的两个输入?

[英]How to add two inputs from the same forms in the same collection field in Meteor?

我正在为我的第一个应用程序制作流星教程。 所以我想再扩展一点,有两个文本框,一个用于评论,一个用于评分。 问题是我无法正确地从两种形式中获取值(实际上根本无法获得额定值)以便将它们保存在我的数据库中,此外,enter-submit功能停止工作。

我的.js身体事件代码是:

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

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

        // Insert a task into the collection
        Meteor.call("addTask", text, rating);

        // Clear form
        event.target.text.value = "";

    }
});

对于添加任务:

AddTask: function(text, rating) {
    //.....
    Tasks.insert({
        text: text,
        createdAt: new Date(),
        owner: Meteor.userId(),
        username: Meteor.user().username,
        rating: rating
    });
}

而我的HTML:

<form class="new-task">
    <h2>What is happening?</h2>
    <input type="text" name="text" placeholder="Share your experience!" />
    <h2>Rating:</h2>
    <input type="text" name="rating" placeholder="insert your rating!" />
</form>

<template name="task">
    <li class="{{#if checked}}checked{{/if}}">
        {{#if isOwner}}
        <button class="delete">&times;</button>
        {{/if}}
        <span class="text"><strong>{{username}}</strong> - {{text}}- {{rating}}</span>
    </li>
</template>

您的流星方法addTask未定义。 呼叫Meteor.call("AddTask", text, rating); 而是将您的方法重命名为addTask

例如:

if (Meteor.isClient) {
    Template.hello.events({
        'click button': function () {
            Meteor.call("addTask", "1", 2, function(error){
                if (error) alert(error.reason);
            });
        }
    });
}

if (Meteor.isServer) {
    Meteor.methods({
        addTask: function (text, rating) {
            check(text, String);
            check(rating, Number);
            console.log(text);
            console.log(rating);
        }
    });
}

暂无
暂无

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

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