简体   繁体   中英

How to check an option in Jquery multi-select dropdown list?

I am unable to check an option from jquery multi-select dropdown list even after being able to find the option in the multi-select list. Please advise me what I am missing. Thanks!!

//this is my multi-select dropdown<br/>
$("#ddlCountries").multiselect();

var country = "USA";

//I tried below, but it did not work    
//$("#ddlCountries").multiselect("widget").find(":checkbox[value='" + country + "']").attr("checked", true);

Response @ Bhavin: I did as per you, still not getting expected result. Please see below.

$(document).ready(function () {
    $("#ddlCountries").multiselect();
    $("#ddlCountries").multiselect('uncheckAll');
}


function ddlRole_OnChange(){
    //client object of ddlRoles combobox
    var roles = $(this).data('tComboBox');
    //selected item from ddlRoles
    var selectedRole = roles.text();

    if (selectedRole.toLowerCase() === "admin"){
        //custom yes/no dialog box (first param is Title, second param is Question, third param is callback function)
        YesNoJqueryDailogBox('Admin Configuration:', 'Need Admin Role ?', 'selectForAdminRole');       
    }  
}

function selectForAdminRole(){

    $(document).ready(function (){
        $("#ddlCountries").multiselect();
        $("#ddlCountries").multiselect('uncheckAll');
        var country = "USA";
        $("#ddlCountries").multiselect("widget").find(":checkbox[value='" + country + "']").prop('checked', true);
    }); 
}

Response @ Ziv Weissman MVC view (.cshtml)

<tr>
    <td align="left" valign="middle" width="30%">
        @if (ViewBag.Roles != null)
        {
         @(
            Html.Telerik().ComboBox().Name("ddlRole") @*server-side combobox name*@
           .BindTo((IEnumerable<SelectListItem>)ViewBag.Roles) @*server-side ddlRole binding in page load*@
           .ClientEvents(events => events.OnChange("ddlRole_OnChange")) @*server-side ddlRole change event*@
          )
        }
    </td>
 </tr>

 <tr>
     <td align="left" valign="middle" width="15%" height="40" >
        Countries <span class="mandatory">*</span>
     </td>
     <td align="left" valign="middle" width="20%" >
        @if (ViewBag.Countries != null)
        {
           @*server-side ddlCountries dropdown binding in page load*@
           @Html.DropDownList("ddlCountries", (IEnumerable<SelectListItem>)ViewBag.Countries)
        }
     </td>
 </tr>

Solved: Refreshing ddlCountries dropdown after checking the option solved the problem.

var countries= $("#ddlCountries").multiselect('uncheckAll');
countries.children('option').each(function (){

    var countryNameText = $(this).text();      
    if (countryNameText == "admin"){
            $(this).attr('selected', 'selected');            
    }
});
$("#ddlCountries").multiselect("refresh"); 

Thanks all of you!

you must refresh multiSelect, and why you add $(document).ready inside selectForAdminRole function

       $("#ddlCountries").find("option[value=" + ce + "]").prop("selected", true)
        $("#ddlCountries").multiselect("refresh")

and you can use this

     $("#ddlCountries").val("USA");
      $("#ddlCountries").multiselect("refresh")                      

If I put onload event twice or setTimeout works. I used the first option as workaround, it's ugly but works fine.

$(document).ready(function() {
   $(document).ready(function() {
      $('#id').multiselect();
   });
});

$(document).ready(function() {
  setTimeout(function() {
      $('#id').multiselect();
   }, 500);
});

jQuery: 1.8.3

Bootstrap: 3.0.3

Multiselect: 0.9.13

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM