小编Dan*_*ard的帖子

动态添加Select2字段以形成

我正在使用带有Meteor和Handlebars的Select2,我正在尝试<input type="hidden"动态添加Select2 字段,以便与Select2一起使用.隐藏字段显示,但Select2注册它们.

HTML看起来像这样.

{{#each update.ingredients}}
  <div class="ingredient">
     <input class="quant span2" type="text" placeholder="Quantity" value="{{quantity}}"/>
     <input class="unit span1" type="hidden" placeholder="Unit" value="{{unit}}"/>
     <input class="ing" type="hidden" placeholder="Ingredient" value="{{ingredient}}"/>
  </div>
{{/each}}
Run Code Online (Sandbox Code Playgroud)

事件处理程序如下所示:

'click .addIngredient': function () {
  $("#input_ingredients").append('<div class="ingredient"><input class="quant span2" type="text" placeholder="Quantity"/><input class="unit span1" type="hidden" placeholder="Unit"/><input class="ing" type="hidden" placeholder="Ingredient"/></div><br>');  
}
Run Code Online (Sandbox Code Playgroud)

创建页面时添加select2:

'click .add': function () {
  $(".tags").select2({
    tags: checkTags(),
    tokenSeparators: [",", " "]
  });
  $(".ing").select2({
    tags: checkIngredients(),
    tokenSeparators: [",", " "]
  });
  $(".unit").select2({
    tags: checkUnits(),
    tokenSeparators: [","]
  });
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感谢!!

jquery meteor jquery-select2

2
推荐指数
1
解决办法
5148
查看次数

标签 统计

jquery ×1

jquery-select2 ×1

meteor ×1