I'm very new to Meteor and following along in the official docs their TODO APP. So far so good but I want to add a second input to the example which has only a place for inputing and storing " text ." I am trying to add a " quantity " field so that a user can input text and quantity.
If I go through the command line and type:
db.tasks.insert({text: "apples", quantity: 4, createdAt: new Date() });
I can get the quantity field to correctly populate. However, I cannot get the data into the quantity field when submitting through the DOM. Here is my HTML:
<head>
<title>Todo List</title>
</head>
<body>
<div class="container">
<header>
<h1>Todo List</h1>
<form class="new-task">
<label for="text">TEXT:</label>
<input type="text" name="text" placeholder="Type to add new tasks" />
<label for="text">NUMBER:</label>
<input type="text" name="quantity" placeholder="Quantity" />
<input type="submit" class="submit"/>
</form>
</header>
<ul>
{{#each tasks}}
<!-- this inserts the template called "task" -->
{{> task}}
{{/each}}
</ul>
</div>
</body>
<template name="task">
<li class="{{#if checked}}checked{{/if}}">
<input type="checkbox" checked="{{checked}}" class="toggle-checked" />
<span class="text">{{text}}{{quantity}}</span>
<button class="delete">×</button>
</li>
</template>
Here is my JS:
Tasks = new Mongo.Collection("tasks");
if (Meteor.isClient){
//This code only runs on client
Template.body.helpers({
tasks: function () {
// Show newest tasks at the top
return Tasks.find({}, {sort: {createdAt: -1}});
}
});
Template.body.events({
"submit .new-task": function (event) {
// Prevent default browser form submit
event.preventDefault();
console.log(event)
// Get value from form element
var text = event.target.text.value;
var quantity = event.target.text.value;
// Insert a task into the collection
Tasks.insert({
text: text,
quantity: quantity,
createdAt: new Date() // current time
});
// Clear form
event.target.text.value = "";
}
});
Template.task.events({
"click .toggle-checked": function () {
// Set the checked property to the opposite of its current value
Tasks.update(this._id, {
$set: {checked: ! this.checked}
});
},
"click .delete": function () {
Tasks.remove(this._id);
}
});
}
You'll see that I have not completed the tutorial and have made some modifications to the HTML.
I'm pretty sure I'm messing up my JS. Any help would be greatly appreciated!
You have:
var text = event.target.text.value;
var quantity = event.target.text.value;
These two are going to be the same.
Instead use:
var text = $('input[name="text"]').val();
var quantity = $('input[name="quantity"]').val();
Just playing around with the JS, I got it to post when I changed this:
// Get value from form element
var text = event.target.text.value;
var quantity = event.target.text.value;
To this:
// Get value from form element
var text = event.target.text.value;
var quantity = event.target.quantity.value;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.