为什么我为这些输入元素接收空值?

时间:2015-12-25 06:09:55

标签: javascript jquery html mongodb meteor

我正在开发基于流星的应用程序,并根据表单输入对我的数据库进行更新。当我在我的mongodb中查找数据时,字段的值被视为空,这不是这种情况,因为我可以看到他们的值在网址中发布,我也是输入它们的人。这是我目前实施的代码。

<template name="room">
   <div class="container">
      <br>
      <br>
      <br>
      <br>
      <h1>{{currentUser.username}} Cards</h1>
      <hr>
      <h2>Fill out the form below to add a card</h2>
      <form class="new-card">
         <input type="text" name="notes" placeholder="Enter some notes on the sprint" />
         <input id="good-checkbox" type="checkbox" name="category" value="Good">Good
         <input id="bad-checkbox" type="checkbox" name="category" value="Bad">Bad
         <!-- <button class="new-card" type="submit">Add Card</button>-->
      </form>
      <div class="col-xs-12 col-sm-12">
         {{#each cards}}
         {{> card}}
         {{/each}}
      </div>
      <br>
      <br>
      <br>
      <h1>Revealed Cards</h1>
      <hr>
   </div>
</template>

Meteor.js:

Router.configure({
    layoutTemplate: 'main-layout'
});
Router.route('/', {
    name: 'home',
    template: 'login'
});
cards = new Mongo.Collection("cards");
if (Meteor.isClient) {
    Router.route('/room', {
        name: 'room',
        template: 'room'
    });
    if (!Meteor.user()) {
        if (Meteor.loggingIn()) { /*we dont want to do anything*/ } else {
            alert("Sorry, you must be logged in first to use our services.");
            Router.go('/');
        }
    }
    Template.room.helpers({
        cards: function() {
            //return all cards in db sort by newest
            return cards.find({}, {
                sort: {
                    createdAt: -1
                }
            });
        }
    });
    Template.body.events({
        "submit .new-card": function(event) {
            //prevent the browsers default behavior for this event
            event.preventDefault();
            var notes = event.target.text.value;
            var good = $('#good-checkbox:checked').val();
            var bad = $('#bad-checkbox:checked').val();
            if (!good && !bad || !notes || notes.length === 0) {
                alert("No fields can be left blank");
            } else {
                cards.insert({
                    notes: notes,
                    good: good,
                    bad: bad,
                    createdAt: new Date(),
                    createdBy: Meteor.user().username
                });
            }
            event.target.text.value = "";
        }
    });
    Template.room.events({
        "click #addCard": function() {
            alert("inserted card");
            cards.insert({
                text: null,
                category: null,
                createAt: new Date(),
                createdBy: Meteor.user().username
            });
        }
    });
    Accounts.ui.config({
        passwordSignupFields: "USERNAME_ONLY"
    });
}

if (Meteor.isServer) {
    Meteor.startup(function() {
        // code to run on server at startup
    });
}

1 个答案:

答案 0 :(得分:0)

正如Shaunak D已在评论中指出,notes事件处理程序中的submit .new-card变量为undefined。使用event.target.notes.value代替event.target.text.value

if (Meteor.isClient) {
    Template.room.helpers({
        cards: function() {
            //return all cards in db sort by newest
            return cards.find({}, {
                sort: {
                    createdAt: -1
                }
            });
        }
    });
    Template.room.events({
        "submit .new-card": function(event) {
            //prevent the browsers default behavior for this event
            event.preventDefault();
            var notes = event.target.notes.value;
            var good = $('#good-checkbox:checked').val();
            var bad = $('#bad-checkbox:checked').val();
            if (!good && !bad || !notes || notes.length === 0) {
                alert("No fields can be left blank");
            } else {
                cards.insert({
                    notes: notes,
                    good: good,
                    bad: bad,
                    createdAt: new Date(),
                    createdBy: Meteor.user().username
                });
            }
            event.target.notes.value = "";
        }
    });
}

这是MeteorPad