JQuery multiselect - Set a value as selected in th

2019-01-06 09:54发布

I am having a multiselect dropdown as below

<select id="data" name="data" class="data" multiple="multiple">
  <option value="100">foo</option>
  <option value="101">bar</option>
  <option value="102">bat</option>
  <option value="103">baz</option>
</select>

On load of page, i will get an array of value like [101,102]. I should iterate through the array and make the values selected(check boxes corresponding to the ids should be checked). Please help.

16条回答
聊天终结者
2楼-- · 2019-01-06 10:23

I ended up having to make a slight change using the click event on the input element by also setting the checked prop after firing the click event.

$(y.Ctrl).multiselect("widget").find(":checkbox").each(function () {
    if ($.inArray(this.value, uniqueVals) != -1) {
        $(this).click();
        $(this).prop('checked', true);
    }
});
查看更多
我命由我不由天
3楼-- · 2019-01-06 10:26

this is my code sample. I have comma separated numbers for multi select drop down and want to select options multiple options from comma separated values.

var selected_tags_arr = new Array();
var selected_tags = 1,2,4;
selected_tags_arr = selected_tags.split(",");
$('#contact_tags option').each(function (){
    var option_val = this.value;
    for (var i in selected_tags_arr) {
        if(selected_tags_arr[i] == option_val){
            $("#contact_tags option[value='" + this.value + "']").attr("selected", 1);
        }
    }
});
查看更多
做自己的国王
4楼-- · 2019-01-06 10:29

It's supposed to be as simple as this:

<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/

查看更多
Evening l夕情丶
5楼-- · 2019-01-06 10:29

This is what worked from me using an array as input. First uncheck all options, then click on the chosen options using jquery

var dimensions = ["Dates","Campaigns","Placements"];

$(".input-dimensions").multiselect("uncheckAll");

for( var dim in dimensions) {
  $(".input-dimensions").multiselect("widget")
    .find(":checkbox[value='" + dimensions[dim] + "']").click();
}
查看更多
Ridiculous、
6楼-- · 2019-01-06 10:29

I hope these functions will help you.

$('#your-select').multiSelect('select', String|Array);

$('#your-select').multiSelect('deselect', String|Array);

$('#your-select').multiSelect('select_all');

$('#your-select').multiSelect('deselect_all');

$('#your-select').multiSelect('refresh');

Reference by this site Reference

查看更多
倾城 Initia
7楼-- · 2019-01-06 10:30

You can try this:

$( "#data" ).val([ "100", "101" ]);
查看更多
登录 后发表回答