如何使用 MultiSelect jQuery 插件添加选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6459974/
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
How to add option using MultiSelect jQuery plugin
提问by Clarence Klopfstein
I am using the jQuery - MultiSelect plugin.
我正在使用 jQuery - MultiSelect 插件。
I want to be able to add an option to my initial select box, and then have the MultiSelect user interface update with the new option.
我希望能够向我的初始选择框中添加一个选项,然后使用新选项更新 MultiSelect 用户界面。
Here is what I have (which doesn't work).
这是我所拥有的(不起作用)。
var value = $("#newGroup").val();
$('#Select1').append("<option value=\"" + value + "\">" + value + "</option>");
Then I've tried to call the same code to recreate the multiselect along with other options like destroying it first.
然后我尝试调用相同的代码来重新创建多选以及其他选项,例如首先销毁它。
Here is the code I use to implement the plugin.
这是我用来实现插件的代码。
$("#Select1").multiselect({ sortable: false, searchable: true });
Here is the plugin's home page: http://quasipartikel.at/multiselect_next/
这是插件的主页:http: //quasipartikel.at/multiselect_next/
回答by D. G.
Do you have the latest version of MultiSelect? Version 1.8 now adds a refresh command that is designed to update lists to reflect newly appended or deleted options. Your code sequence should be:
你有最新版本的 MultiSelect 吗?1.8 版现在添加了一个刷新命令,旨在更新列表以反映新添加或删除的选项。您的代码序列应该是:
var value = $("#newGroup").val();
$('#Select1').append("<option value=\"" + value + "\">" + value + "</option>");
$('#Select1').multiselect( 'refresh' );
回答by elf
try to use
尝试使用
$('.multiselect').multiselect('destroy');
$('.multiselect').multiselect();
回答by asologor
For old versions you can try this
对于旧版本,你可以试试这个
$('select').multiselect('destroy').removeData().multiselect();
'destroy' method doesn't remove data from the select node so when you try to initialize it again it thinks that it already initialized. removeData() solves the problem.
'destroy' 方法不会从选择节点中删除数据,因此当您尝试再次初始化它时,它认为它已经初始化。removeData() 解决了这个问题。
New versions has 'refresh' method so you can append some options to the select and call
新版本具有“刷新”方法,因此您可以将一些选项附加到选择和调用
$('select').multiselect('refresh');
when multiselect already initialized.
当多选已经初始化。
回答by dhruvil
Even i was stuck into something like this means when i appended the option into select after that it wasn't updating the multiselect so i got this below solution.
即使我陷入了这样的事情,这意味着当我将选项附加到 select 之后,它没有更新多选,所以我得到了下面的解决方案。
$('#Select1').append("<option value=\"" + value + "\">" + value + "</option>");
After this add this line.
在此之后添加这一行。
$('#Select1').multiselect('rebuild');
And it will rebuild the multiselect with the newest data added also.
并且它将使用添加的最新数据重建多选。