简体   繁体   English

MeteorJS中的Select2选项

[英]Select2 options in MeteorJS

I am trying to instantiate a multi-select box with Meteor JS and Select2. 我正在尝试使用Meteor JS和Select2实例化多选框。 My html resides in a template as follows: 我的html驻留在模板中,如下所示:

<template name="selectbox">
     <select id="testbox">
         <option value="AL">Alabama</option>
         <option value="AL">Washington</option>
     </select>
</template>

Within my client js I have the following code: 在我的客户端js中,我有以下代码:

Template.selectbox.rendered = function(){
    var options = { allowClear : true, multiple : true }
    $("#testbox").select2(options);
};

Unfortunately my browser's console produces the following error: 不幸的是,我的浏览器控制台产生以下错误:

Exception from Deps afterFlush function: Error: Option 'multiple' is not allowed for Select2 when attached to a <select> element.

I have also tried placing the following code directly in my client js file (as well as within Template.selectbox.create): 我还尝试过将以下代码直接放在我的客户端js文件中(以及在Template.selectbox.create中):

$(document).ready(function(){
    var options = { allowClear : true, multiple : true }
    $("#testbox").select2(options);
});

Unfortunately I receive the same error. 不幸的是,我收到同样的错误。 The peculiar thing is I receive no error if my options are simply: 奇怪的是,如果我的选择很简单,我不会收到任何错误:

var options = { allowClear : true, placeholder : 'test' }

Neither option appears to take affect. 两种选择似乎均未生效。 However, The rendering of the select box is that of selct2. 但是,选择框的呈现方式是selct2的呈现方式。

Any idea what could be the issue? 知道可能是什么问题吗? Thanks in advanced. 提前致谢。

See how the example on select2 website is made. 查看select2网站的示例 According to that code, you should add multiple attribute to the select tag, not to js method parameters. 根据该代码,您应该将multiple属性添加到select标签,而不是js方法参数。

So in your template replace <select id="testbox"> with <select multiple id="testbox"> . 因此,在模板中,将<select id="testbox">替换为<select multiple id="testbox">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM