我有一个可以添加输入字段的动态表单:
Template.decisionSetUp.events({ 'click #addInput':function() { event.preventDefault(); $('.categoryContainer').append('<input type="text" name="categoryName" class="form-control categoryTitle" placeholder="Kategorien">') } });现在我想获取输入字段的值并将它们推入集合内的数组中。 问题是我不知道如何正确填充数组? 我错过了什么?
Template.decisionSetUp.events({ 'click #start':function(event,t){ event.preventDefault(); $('.categoryTitle').each(function() { var title = $(this).val(); categoryTitle[] = title; }); Questions.insert({ category: categoryTitle, });之后,我把它发送到DOM。
Template.decision.helpers({ category: function(){ return this.category; } })HTML
<template name="decision"> {{#each category}} {{this}} {{/each}} </template>如果你能帮助我,那会很棒!
I have a dynamic form where you can add input fields:
Template.decisionSetUp.events({ 'click #addInput':function() { event.preventDefault(); $('.categoryContainer').append('<input type="text" name="categoryName" class="form-control categoryTitle" placeholder="Kategorien">') } });Now i want to get the values of the input fields and push them into an array inside of a collection. The problem is that i dont know how to fill the array correctly?! What am i missing???
Template.decisionSetUp.events({ 'click #start':function(event,t){ event.preventDefault(); $('.categoryTitle').each(function() { var title = $(this).val(); categoryTitle[] = title; }); Questions.insert({ category: categoryTitle, });After this i send it to the DOM.
Template.decision.helpers({ category: function(){ return this.category; } })HTML
<template name="decision"> {{#each category}} {{this}} {{/each}} </template>Would be great if you could help me!
最满意答案
您没有将您的值添加到categoryTitles数组中。
这应该可以解决您的问题:
Template.decisionSetUp.events({ 'click #start': function(event, t) { event.preventDefault(); var categoryTitles = []; $('.categoryTitle').each(function(index) { var title = $(this).val(); categoryTitles[index] = title; }); Questions.insert({ category: categoryTitles }); } });这是一个MeteorPad 。
此外,我注意到您的#addInput事件处理程序缺少event参数。
You are not adding your values to the categoryTitles array.
This should fix your problem:
Template.decisionSetUp.events({ 'click #start': function(event, t) { event.preventDefault(); var categoryTitles = []; $('.categoryTitle').each(function(index) { var title = $(this).val(); categoryTitles[index] = title; }); Questions.insert({ category: categoryTitles }); } });Here is a MeteorPad.
Furthermore, I noticed that your #addInput event handler is missing the event parameter.
更多推荐
发布评论