循环访问Array并添加到Meteor的集合中(Looping through Array and add into collection with Meteor)

编程入门 行业动态 更新时间:2024-10-25 23:22:24
循环访问Array并添加到Meteor的集合中(Looping through Array and add into collection with Meteor)

我有一个可以添加输入字段的动态表单:

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.

更多推荐

本文发布于:2023-07-26 05:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1271359.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Meteor   Array   Looping   collection   add

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!