Set values in jquery multiselect dropdown

試著忘記壹切 提交于 2019-12-06 00:59:20

问题


I want to set the multiple ITEMS in jQuery multiselect dropdown depending on the VALUE of that item, which I'm getting from database, separated by ",".

I'm saving this fetched data in hidden field.

Ex.

Hidden field: hdnLots = 64,65 , Items for lot No.: 64 = Lot 1, 65 = Lot2

So when I get the hdnLot=65, then in jQuery Multiselect dropdown only Lot2 needed to be selected. And same for 64,65. i.e. When multiple values are selected than all those values needed to seleted.

I've tried: JQuery multiselect - Set a value as selected in the multiselect dropdown

Code I've wrote for this is:

if ($("#<%= btnUpdateProject.ClientId %>").css('display') == "inline-block") 
{
   debugger;
   var dataarray = document.getElementById('<%= hdnLots.ClientId %>').value.split(",");
   $("#<%= ddlNoOfLots.clientid %>").val(dataarray);
}

回答1:


I hope this will help you:

Demo

$(document).ready(function() {
$("select").multiselect({
   selectedText: "# of # selected"
});
var hidValue = $("#hidSelectedOptions").val();
alert(hidValue);
var selectedOptions = hidValue.split(",");
for(var i in selectedOptions) {
    var optionVal = selectedOptions[i];
    $("select").find("option[value="+optionVal+"]").prop("selected", "selected");
}
$("select").multiselect('reload');
});

EDIT

refresh has been removed from latest jQuery-MultiSelect. Using reload will solve the question now.




回答2:


The one thing you would need to ensure is that the values in the array are strings:

<select id='multipleSelect' multiple='multiple'>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>
<script type='text/javascript'>
    $('#multipleSelect').val(['1', '2']);
</script>

Check my Fiddle: https://jsfiddle.net/luthrayatin/jaLygLzo/




回答3:


var selectedOptions = hidValue.split(",");
typeof (selectedOptions != 'undefined' && $("#hidSelectedOptions").multiselect('select', selectedOptions));


来源:https://stackoverflow.com/questions/20917049/set-values-in-jquery-multiselect-dropdown

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