How to Set Selected value in Multi-Value Select in Jquery-Select2.?

后端 未结 14 869
轻奢々
轻奢々 2020-12-01 01:41

hi all i am binding my dropdown with Jquery-Select2. Its working fine but now i need to Bind my Multi-Value selectBox by using Jquery-Select2.

M

相关标签:
14条回答
  • 2020-12-01 01:54

    Using select2 library there are 2 ways to set the value 1. when single value is present you can pass it as string

    $("#elementid").select2("val","valueTobeset")
    

    2. when you are using select2 with multiselect option you can pass an array of the values to select2 and all those values will be set

    var arrayOfValues = ["a","c"]
    $("#elementid").select2("val",arrayOfValues)
    

    remember that you can set single value on multiselect by passing an array also like this

    var arrayOfValues = ["a"]
    $("#elementid").select2("val",arrayOfValues)
    
    0 讨论(0)
  • 2020-12-01 01:59
    This is with reference to the original question
    $('select').val(['a','c']);
    $('select').trigger('change');
    
    0 讨论(0)
  • 2020-12-01 02:02

    This doesn't work. only one value is ever pre-selected even though both options are available in the list only the first is shown ('#searchproject').select2('val', ['New Co-location','Expansion']);

    0 讨论(0)
  • 2020-12-01 02:02

    Use multiselect function as below.

    $('#drp_Books_Ill_Illustrations').multiSelect('select', 'value');
    
    0 讨论(0)
  • 2020-12-01 02:05

    So I take it you want 2 options default selected, and then get the value of it? If so:

    http://jsfiddle.net/NdQbw/1/

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

    And to get value:

    alert($(".leaderMultiSelctdropdown").val());
    

    To set the value:

     $(".leaderMultiSelctdropdown").val(["a", "c"]);
    

    You can also use an array to set the values:

    var selectedValues = new Array();
    selectedValues[0] = "a";
    selectedValues[1] = "c";
    
    $(".Books_Illustrations").val(selectedValues);
    

    http://jsfiddle.net/NdQbw/4/

    0 讨论(0)
  • 2020-12-01 02:05

    Just look at this one it will helps.

    var valoresArea=VALUES // it has the multiple values to set, separated by comma
    var arrayArea = valoresArea.split(',');
    $('#area').val(arrayArea);
    

    the URL is- link

    0 讨论(0)
提交回复
热议问题