How to add option using MultiSelect jQuery plugin

a 夏天 提交于 2019-12-04 03:42:02

问题


I am using the jQuery - MultiSelect plugin.

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.

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/


回答1:


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:

var value = $("#newGroup").val();
$('#Select1').append("<option value=\"" + value + "\">" + value + "</option>");
$('#Select1').multiselect( 'refresh' );



回答2:


try to use

$('.multiselect').multiselect('destroy');
$('.multiselect').multiselect();



回答3:


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.

New versions has 'refresh' method so you can append some options to the select and call

$('select').multiselect('refresh');

when multiselect already initialized.



来源:https://stackoverflow.com/questions/6459974/how-to-add-option-using-multiselect-jquery-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!