how to fetch selected value into select2 in my Edit From using ajax (codeigniter)

I create one form which includes select2 control.and i have data in database.

Now I want to fetch value of particular data value into select2 control when edit the form. My selected select value is store in one variable Now i want that value dispaly in select2 control inside the edit form and i dont know how ..

here is my edit form code:

<div id="editm" class="modal fade" tabindex="-1" role="dialog">
 <div class="modal-dialog modal-sm" role="document">
<div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    <h4 class="modal-title">Update Rayon</h4>
  <div class="modal-body">
    <form id="validate1" method="POST">
      <div class="form-group">
       <label class="control-label">Kode Rayon</label>
        <input type="text" class="form-control" id="edit_Kode_rayon" name="edit_Kode_rayon" placeholder="Kode Rayon" readonly>

          <div class="form-group">
            <label class="control-label">Nama Rayon</label>
              <input type="text" class="form-control" id="edit_nama_rayon" name="edit_nama_rayon" placeholder="Nama Center" >
          <div class="form-group">
            <label class="control-label">Nama Region</label>
              <select class="form-control kode_region" id="nRegionE" name="kode_region" style="width: 100%;">
                <option value=""></option>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      <button type="button" id="update" class="btn btn-primary">Update</button>

here is jquery code for edit button:

$(document).on("click", "#edit", function(e){
   var editid = $(this).attr("value");
      url: "<?php echo base_url();?>Rayon/editdata",
      type: "POST",
      dataType: "json",
      data:{editid: editid},
      success: function(data){
      if(data.responce == "success"){
         //$("#nRegionE").val(data.posts.kode_region);<-- I TRIED LIKE THIS .. NOT WORK -->
         //$("#nRegionE").select2().select2('val',data.posts.kode_region);<-- I TRIED LIKE THIS . NOT WORK-->
         $('#nRegionE').val(data.posts.kode_region).trigger("change");<-- EVEN THIS ONE IS NOT WORK -->

just to make it clear i took a screenshot of my edit form:

enter image description here

all what i want is to fetch the value of data into select2 form control of Edit Form .. can anyone help me to do that ? n i'll be so thankful.

Read more here:

Content Attribution

This content was originally published by Jafar at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: