2

I am using the following jquery code to get data from database-

$.get('loadTaskDetails' + $taskId, {taskId: $taskId}, function(data) {
                    $.each(data, function(i, data) {$('#ddl_Deliverables').find('option[value=data.tasks_deliverable_id]').attr('selected', true);    
                });
                }, 'json');

I have a a dropdown (ddl_Deliverables) which already contains many options. I want the option value to be set selected with the macthing value coming from database through $.get method.

0

2 Answers 2

2

This doesn't interpret the variable therein:

find('option[value=data.tasks_deliverable_id]')

That string value is a literal string. It's specifically looking for a value called "data.tasks_deliverable_id". To use the data variable, you need it to be outside the string:

find('option[value=' + data.tasks_deliverable_id + ']')
0
0
$.get('loadTaskDetails' + $taskId, {taskId: $taskId}, function(data) {
     $.each(data, function(i, data) {
        $('#ddl_Deliverables option').removeAttr('selected');
        $('#ddl_Deliverables').find('option[value="' + data.tasks_deliverable_id + '"]').attr('selected', 'selected');    
     });
}, 'json');

Not the answer you're looking for? Browse other questions tagged or ask your own question.