jQuery Select2 示例不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15748549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:44:33  来源:igfitidea点击:

Select2 example is not working

javascriptjqueryjquery-select2

提问by user1740381

I am trying to use Select2.js plugin. I have created a fiddle of the simplest example of select2 but i can't figure out why this is not working ?

我正在尝试使用Select2.js 插件。我已经创建了一个最简单的 select2 示例的小提琴,但我不明白为什么这不起作用?

http://jsbin.com/edeseh/2/edit

http://jsbin.com/edeseh/2/edit

you can see in the fiddle that selection of items in selectlist and allow clear is not working.

您可以在小提琴中看到在选择列表中选择项目并允许清除不起作用。

Can anybody please help me, what i am doing wrong here ?

任何人都可以帮助我,我在这里做错了什么?

回答by Gaurav

You should use options value to enable selection

您应该使用选项值来启用选择

and

you should use an empty option for showing placeholder and allowClearoption.

您应该使用空选项来显示占位符和allowClear选项。

Here is the working fiddle :

这是工作小提琴:

http://jsbin.com/edeseh/8/edit

http://jsbin.com/edeseh/8/edit

Main code copied from the link:

从链接复制的主要代码:

HTML:

HTML:

<select id="e2" style="width:200px;">
  <option value=""><option>
  <option value="1">Mustard</option>
  <option value="2">Ketchup</option>
  <option value="3">Relish</option>
</select>

JavaScript:

JavaScript:

$("#e2").select2({
  placeholder: "Select a State",
  allowClear: true
});

回答by Kelvin Barsana

Older versions require you to add 'select2' class in the dropdown.

旧版本要求您在下拉列表中添加“select2”类。