ajax代码没有将数据从表转到第二行

hi guys i have an issue on ajax. where ajax can't work to take the data of second rows.

This's code in model

 function getdtbarang($barcode = FALSE) {
         if ($barcode === FALSE)
        {
                $query = $this->db1->get('tblmstbarang');
                return $query->result_array();
        }

        $query = $this->db1->get_where('tblmstbarang', array('barcode' => $barcode));
        return $query->row_array();
        }

This's code in controller:

 function getdatabarang() {
        $barcode = $this->input->post('barcode');
        $data=$this->Mbarang->getdtbarang($barcode);
        echo json_encode($data);
        }

This's ajax code in view. NB:some code has updated

     <script type="text/javascript">

   function getdatabrg(barcode){
    $('#barcode_2').each(function() {    
        var barcode =$('#barcode_2').val();   
       $.ajax({
            type    : "post",
            data    : "barcode=" +barcode,                       
            url     : "<?php echo base_url();?>index.php/master/barang/Cbarang/getdatabarang",
            dataType:"json",
            success: function(data){ 
            for(var i in data)
              {
            var obj= data[i];          


                $("#barang_nama_2").val(obj.barang_nama);
                $("#harga_beli_2").val(obj.harga_beli);

          }}

        }); 


    });
}


    $(document).ready(function() { 
     $('#barcode_2').keyup(function() {
      getdatabrg(); 
    });
  });
      </script>
    <td><input type="text" class="form-control" id="barcode"    name="barcode[]"     value="<?php echo set_value('barcode['.$i.']') ;?>" onKeyUp="getValues()" ></td>
    <td><input type="text" class="form-control" id="barang_nama" name="barang_nama[]" value="<?php echo set_value('barang_nama['.$i.']') ;?>" onKeyUp="getValues()"  disabled></td>
    <td><input type="text" class="form-control" id="harga_beli"  name="harga_beli[]"  value="<?php echo set_value('harga_beli['.$i.']');?>" onKeyUp="getValues()" disabled></td>

    <td><input type="text" class="form-control" id="barcode_2"        name="barcode[]"     value="<?php $a=set_value('barcode[0]') ; echo $a;?>"></td>
    <td><input type="text" class="form-control" id="barang_nama_2"    name="barang_nama[]" value="<?php $a=set_value('barang_nama[0]') ; echo $a;?>" onKeyUp="getValues()" readonly></td>
    <td><input type="text" class="form-control" id="harga_beli_2"     name="harga_beli[]"  value="<?php $a=set_value('harga_beli[0]'); echo $a;?>" onKeyUp="getValues()"  readonly></td>

    <td><input type="text" class="form-control" id="barcode_3"      name="barcode[]"     value="<?php echo $detail->barcode;?>"></td>
    <td><input type="text" class="form-control" id="barang_nama_3"  name="barang_nama[]" value="<?php echo $detail->barang_nama;?>" onKeyUp="getValues()" readonly></td>
    <td><input type="text" class="form-control" id="harga_beli_3"   name="harga_beli[]"  value="<?php echo $detail->harga_beli;?>"  onKeyUp="getValues()" readonly></td>

i hope getting solve for this problem. many thanks for all your response.

Like Zerfiryn mentioned : "You cannot use the same id for html elements. jQuery will only fetch the first element"

solutions: you can use the same class multiple times on html elements. Call not the id in your ajax but the class form-control. So, replace $("#barcode") with $(".form-control")

writing an answer cause I can't place comments -.-

You have not constructed model correctly your model is directly outputting the data which so you will end up with the output something like

[{barang_name:"value"},{harga_beli:"value"}][{barang_name:"value"},{harga_beli:"value"}][{barang_name:"value"},{harga_beli:"value"}]...

which is not correct JSON your should output correct JSON your output should be [ [{barang_name:"value"},{harga_beli:"value"}], [{barang_name:"value"},{harga_beli:"value"}],.. ] which is array of arrays

The simplest way to achieve this is

1 ) get the desired data from your model

function your_model_function()
{
   $result = execute_query($sql);
   $rows = array();
   if($result->num_rows)
   {
     while($row = $result->fetch_row())
     $rows[] = $row;
   }
   return $rows;
}

2) use json_encode to encode your whole data array in json format

 $data = $this->model->your_model_func();
 echo json_encode($data);

3) Use the JSON in your success callback function

 //writing success function of ajax
     function(data)
    {
      for(var i in data)
      {
         var row = data[i];
         // to acccesss the data of row ie json object you can use . operator
         console.log(row.key)
         // manipulate dom content.
      }
    }

NOTE: i have written a pseudo code only you cannot directly copy paste it to get result it's just to clear your mind.