How to set selected value in dropdown through javascript

This question already has an answer here:

  • Change the selected value of a drop-down list with jQuery 15 answers

  • 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 + ']')
    

    $.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');
    
    链接地址: http://www.djcxy.com/p/83908.html

    上一篇: JQuery从属性的值中设置<select>索引

    下一篇: 如何通过javascript在下拉菜单中设置选定的值