dppcyt6157 2016-12-29 07:34
浏览 19

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.

  • 写回答

2条回答 默认 最新

  • doubei8541 2016-12-29 09:15
    关注

    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 -.-

    评论

报告相同问题?

悬赏问题

  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题