Firstly, apologize you find this really duplicates with other questions but since I'm already try most of the solutions given, but still stuck in the middle, then this is my last choice for getting the solutions. I've try the examples like this:
var user_dept = val[0].user_dept;
$('#user_dept option:selected').attr('value', user_dept);
and
var user_dept = val[0].user_dept;
$('#user_dept option[value="'+user_dept+'"]').attr('selected', true);
and as well this:
$('#user_dept').append($('<option>',{
value: val[0].user_dept,
}));
But couldn't get the issues solved. FYI, I declare all these codes inside if statement on jQuery. Wish to get the answers for jquery if possible since I'm using jquery for my work.
Edited:
As I mention on these comments below, I get the select option generated by this function,
function UserDept()
{
$.getJSON('user_management_lib.php?mode=getDepartmentList', function(data){
var list = [];
$.each(data, function(index, value){
list += '<option value="'+value['department_id']+'">'+value['department']+'</option>'
});
$('#user_dept').html(list);
});
}
But as for selected option, I need to get this done by value which are equal to user_dept column from distinct table which is totally unrelated to function above. Since I use jQuery and array, I will declare this in $('#user_dept').val(val[0].user_dept);. I will clarify if necessary. Sorry for any inconveniences.
Edited on 30/12/2016: Code below is the callback function using ajax where this question also included. So, it's quite incomplete since I'm playing around to fix this.
$.ajax({
cache: false,
url: 'ldap_lib.php',
data:'mode=findLDAPServerInfo&ldapid=<?php echo $target_lid; ?>',
type:'POST',
dataType:'json',
success: function(val){
UserDept();
UserRole();
GlobalDept();
$('#mode').val('editLDAPServer');
$('#l_name').val(val[0].l_name);
$('#l_desc').val(val[0].l_desc);
$('#l_ip1').val(val[0].l_ip1);
$('#l_port1').val(val[0].l_port1);
$('#l_ip2').val(val[0].l_ip2);
$('#l_port2').val(val[0].l_port2);
$('#l_domainname').val(val[0].l_domain);
$('#l_loginname').val(val[0].l_loginname);
$('#l_loginpwd').val(val[0].l_loginpwd);
$('#l_mobile').val(val[0].l_mobile);
$('#l_basedn').val(val[0].l_basedn);
$('#l_filter').val(val[0].l_filter);
$('#l_type').val(val[0].l_type);
$('#l_loginmode').val(val[0].l_loginmode);
$('#l_scope').val(val[0].l_scope);
$('#sync_frequency').val(val[0].sync_frequency);
$('#sync_time').val(val[0].sync_hour);
if(val[0].sync_ul == 't'){
$('#l_sync_ul').prop('checked', true);
$('#tip_ul_dept').attr('style','display:block');
var user_dept = val[0].user_dept;
var user_role = val[0].user_role;
//$('#user_dept option[value="'+user_dept+'"]').attr('selected', true);
//$('#user_role option[value="'+user_role+'"]').attr('selected', true);
//$('#user_dept option:selected').attr('value', user_dept);
console.log($("#user_dept option"));
} else{
$('#l_sync_ul').prop('checked', false);
$('#tip_ul_dept').attr('style','display:none');
}
if(val[0].sync_gab == 't'){
$('#l_sync_gab').prop('checked', true);
$('#tip_gab_dept').attr('style','display:block');
// $('#gab_dept').val(val[0].gab_dept);
} else{
$('#l_sync_gab').prop('checked', false);
$('#tip_gab_dept').attr('style','display:none');
}
//$('#gab_dept').val(val[0].gab_dept);
$('#user_dept').val(val[0].user_dept);
},
error: function(){
alert('Failed To Retrieve Server');
}
});