r k

r k

  • 2k
  • 49
  • 1.4k

Html select option Multi select option

Apr 17 2021 1:40 AM
I tried all the methods with hard-coded ,the values i am getting selected for options highlighted, but when i replace hard coded value with hidden values (actual user pre selected values from the database )
 
it just shows /high light the first value
 
Please help this is urgent.
 
below is my code:
  1. $(function () {   
  2. var hdnvalues = "";    
  3. if ($.trim($('#<%=hfroles.ClientID %>').val() != ""))   
  4. { hdnvalues = $.trim($('#<%=hfroles.ClientID %>').val());  }       
  5.  var values = "1,2,3";    
  6.  // var values = hdnvalues;    
  7.   $.ajax({     
  8.       type: "POST",   
  9.                 url: "NotificationsNews.aspx/GetRoles",    
  10.                  data: '{}',      
  11.                 contentType: "application/json; charset=utf-8",    
  12.                  dataType: "json",    
  13.                  success: function (r) {   
  14.                  var ddlRoles = $("[id*=ddlRoles]");   
  15.                     $.each(r.d, function ()   
  16.                    {   
  17.                      ddlRoles.append($("<option></option>").val(this['Value']).html(this['Text']));  
  18.                          $.each(values.split(","), function (i, e) {   
  19.                             $("#ddlRoles option[value='" + e + "']").prop("selected"true);   
  20.                          });      
  21.                   });   
  22.                 }      
  23.          });  
  24.  });

Answers (2)