gpt4 book ai didi

javascript - ajax代码不将数据从表中取出到第二行

转载 作者:行者123 更新时间:2023-12-03 05:21:13 25 4
gpt4 key购买 nike

大家好,我有一个关于 ajax 的问题。其中ajax无法获取第二行的数据。

这是模型中的代码

 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();
}

这是 Controller 中的代码:

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

这是 View 中的 ajax 代码。注意:一些代码已更新

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

我希望这个问题能够得到解决。非常感谢您的回复。

最佳答案

就像 Zerfiryn 提到的:“你不能对 html 元素使用相同的 id。jQuery 只会获取第一个元素”

解决方案:您可以在 html 元素上多次使用同一个类。不要调用 ajax 中的 id,而是调用 form-control 类。因此,将 $("#barcode") 替换为 $(".form-control")

写答案因为我无法发表评论-.-

关于javascript - ajax代码不将数据从表中取出到第二行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41375158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com