如何在 html 中添加对 div 的阿贾克斯响应

如何在 HTML 中将阿贾克斯响应附加到 div 中


这是我的阿贾克斯代码


$(document).ready(function(){

    $('.credit,.debit').change(function(){

        var value=$(this).val();

        $.ajax({

            type:"POST",

            url:"<?php echo base_url();>index.php/Transactions/getamountbyid",

            data:{id:value},

            success:function(result){

                console.log(result);

                $(".bal").html(result);

            }

        });

    });

});

这是我的控制器代码


public function getamountbyid(){

    $id=$this->input->post('id');

    $this->Transaction_model->getamountbyid($id);

    print_r($data1);

}

这是我的阿贾克斯回应


Array ( [0] => stdClass Object ( [Accounts_id] => 1 [Accounts_number] => 123123123 [Account_Name] => Qualight [account_nickname] => Qualight [Address] => hyderabad [Mobile] => 9123912345 [account_type] => 1 [PAN_NO] => 1231231231 [GST_NO] => 123 [credit] => 20000.00 [debit] => [balance] => 20000.00 [Time_stamp] => 2020-02-13 18:51:49 ) )

我想显示帐户借方和贷方余额后从下拉列表中选择它应该显示在下面的下拉列表,请帮助我如何将所需的响应数据附加到必填字段或div


浮云间
浏览 167回答 3
3回答

慕容708150

首先,如果返回不起作用,请更改为“回声”,然后尝试 echo。接下来,更改为print_r($data1);return json_encode($data1);$(".bal").html(result);$(".bal").html( result[0].credit+"<br>"+result[0].debit);

紫衣仙女

如果你只想追加,那么你需要使用 append() 函数。否则,请同时提供HTML,以便我了解您的要求和概念

qq_花开花谢_0

您需要创建一个具有标识符的 div,以便在其中显示贷方/借方,并在 ajax 响应中填充此 div。首先,使用json_encode()public function getamountbyid(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$id=$this->input->post('id');$this->Transaction_model->getamountbyid($id);**print_r(json_encode($data1));**&nbsp; &nbsp; }然后,使用$("#target-div").append(JSON.stringify(data));success: function (data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(var key in data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var value = data[key];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; html+='<div>'+key+':'+value+'</div>'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#target-div").append(html);
打开App,查看更多内容
随时随地看视频慕课网APP