dongnachuang6635 2016-08-18 21:35
浏览 66
已采纳

使用Ajax获取已发布的json数据的值

When I make an ajax call, I get the ID of my input and I add the id to my json data.

I need to get this id to use it id my success function.

IE : In data id = 1, i need in success to get .qte1

If you have any idea... :)

$('#action-button').click(function() {
    $.ajax({
        type: 'POST',
        url: 'get_qty.php',
        data: {
            'id': $('input[name=qte]').val(),
        },
        success: function(data) {
            $(".qte").html("");
            $(".qte").append(data);

        }
    });
    return false;
});
  • 写回答

1条回答 默认 最新

  • douniuta4783 2016-08-18 21:55
    关注

    If I understood you correctly you can just concat the value as such

    var data = JSON.parse(data);
    var $div = "#qte" + data.id;
    $($div).html("test");
    

    I have converted to json object because if you don't have access to the php file this at least converts from string to object. Make sure you are using a #to call a specific div not a class.

    This is my PHP:

    header('Content-type: application/json');
    $arr = array("id"=> $_POST['id'],"qty"=> "1");
    echo json_encode($arr);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)