This blog post all about How to Select Option Value In Ajax Success Call in Jquery. Its very simple and easy bit of code. here is the below example.
$(‘#id option[value=”‘+par+'”]’).attr(“selected”, “selected”);
Step-1:
HTML for example:-
1 2 3 4 5 6 7 8 9 10 | <div class="col-sm-6"> <div class="form-group"> <label for="message-text" class="col-form-label">Task Status:</label> <select class="form-control" name="work_status" id="work_status"> <option value="">---Select---</option> <option value="1">Completed</option> <option value="2">Partially Completed</option> <option value="3">Not started yet</option> </select> </div></div> |
Step-2:
Example for get value in Ajax response -:
1 2 3 4 5 6 7 8 9 10 11 12 13 | $.ajax({ url: 'data.php', type: 'POST', dataType: 'JSON', data: {aid:aid}, success: function (data) { var =1;//let say it. $('#work_status option[value="'+var+'"]').attr("selected", "selected"); }, error: function (e) { console.log("error") } }) |
Hope it helps you…