所有我绑定我的下拉与Jquery-Select2。它的工作正常,但现在我需要使用Jquery-Select2绑定我的多值selectBox。
我的DropDwon
<div class="divright"> <select id="drp_Books_Ill_Illustrations" class="leaderMultiSelctdropdown Books_Illustrations" name="drp_Books_Ill_Illustrations" multiple=""> <option value=" ">No illustrations</option> <option value="a">Illustrations</option> <option value="b">Maps</option> <option value="c">Portraits</option> </select> </div>
从这个链接http://ivaynberg.github.com/select2/我使用Mutiple值选择框,我可以绑定我的dropdwon
$("dropdownid").Select2()
它的工作正常,但现在我需要得到该选定的值到我的下拉编辑模式所以我使用这个例子
$(".Books_Illustrations").select2("val",["a","c"]);
它的工作,但如何解决我的选择,因为用户可以选择任何东西所以我不能写一个,静态地说,为什么我需要动态绑定我的选择值在编辑模式。
我想现在你们都清楚了我的要求。请让我知道,如果你需要进一步清除。
解决方法
实际上你只需要$ .each来获取所有的值,这将帮助你
jsfiddle.net/NdQbw/5
<div class="divright"> <select id="drp_Books_Ill_Illustrations" class="leaderMultiSelctdropdown Books_Illustrations" name="drp_Books_Ill_Illustrations" multiple=""> <option value=" ">No illustrations</option> <option value="a" selected>Illustrations</option> <option value="b">Maps</option> <option value="c" selected>selectedPortraits</option> </select> </div> <div class="divright"> <select id="drp_Books_Ill_Illustrations1" class=" Books_Illustrations" name="drp_Books_Ill_Illustrations" multiple=""> <option value=" ">No illustrations</option> <option value="a">Illustrations</option> <option value="b">Maps</option> <option value="c">selectedPortraits</option> </select> </div> <button class="getValue">Get Value</button> <button class="setValue"> Set value </button> <div class="divright"> <select id="drp_Books_Ill_Illustrations2" class="leaderMultiSelctdropdown Books_Illustrations" name="drp_Books_Ill_Illustrations" multiple=""> <option value=" ">No illustrations</option> <option value="a" selected>Illustrations</option> <option value="b">Maps</option> <option value="c" selected>selectedPortraits</option> </select> </div> <div class="divright"> <select id="drp_Books_Ill_Illustrations3" class=" Books_Illustrations" name="drp_Books_Ill_Illustrations" multiple=""> <option value=" ">No illustrations</option> <option value="a">Illustrations</option> <option value="b">Maps</option> <option value="c">selectedPortraits</option> </select> </div> <button class="getValue1">Get Value</button> <button class="setValue1"> Set value </button>
剧本:
var selectedValues = new Array(); selectedValues[0] = "a"; selectedValues[1] = "c"; $(".getValue").click(function() { alert($(".leaderMultiSelctdropdown").val()); }); $(".setValue").click(function() { $(".Books_Illustrations").val(selectedValues); }); $('#drp_Books_Ill_Illustrations2,#drp_Books_Ill_Illustrations3').select2(); $(".getValue1").click(function() { alert($(".leaderMultiSelctdropdown").val()); }); $(".setValue1").click(function() { //You need a id for set values $.each($(".Books_Illustrations"),function(){ $(this).select2('val',selectedValues); }); });