【问题标题】:Looping through Array and add into collection with Meteor循环遍历数组并使用 Meteor 添加到集合中
【发布时间】:2016-03-27 20:42:54
【问题描述】:

我有一个动态表单,您可以在其中添加输入字段:

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>

如果你能帮助我,那就太好了!

【问题讨论】:

  • 现在发生了什么不工作?

标签: javascript jquery arrays meteor


【解决方案1】:

您没有将值添加到 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 参数。

【讨论】:

  • 非常感谢!拯救了我的一天:)
猜你喜欢
  • 2021-09-12
  • 2020-09-10
  • 1970-01-01
  • 2018-04-07
  • 2022-01-27
  • 1970-01-01
  • 1970-01-01
  • 2018-03-13
  • 1970-01-01
相关资源
最近更新 更多