douchuifk90315 2017-06-23 06:40
浏览 278
已采纳

使用.html()不显示JSON数据

Before posting this question, I did search here and got different answers and I think it doesn't fit to my needs. I have a button when clicked, the following js script will run

$("#update-details-btn").on('click', function() {
        $.ajax({
            type: 'post',
            dataType: 'json',
            data: "confirmation="+get_data,
            url: '../for_update_details',
            success: function(data)
            {
                console.log(data);
                $('div#update_details_body').html(data.results);

and this is the container

<div id="update_details_body" class="modal-body"></div>

the data comes from a php function

$data['results'] = NULL;
 $results = "<div class='form-group'>";
 $results .= "<label for='document-type' class='col-sm-4 control-label'>Category</label>";
 $results .= "</div>";
 $data['results'] = $results;
 echo json_encode($data);

As you can see from the js, I did a console.log which prints exactly what the data.results contain but when I use .html() to put inside the container, nothing happens. What could be the culprit here? I am doing this coding to other functions but only this section is not working

  • 写回答

4条回答 默认 最新

  • dtlrp119999 2017-06-23 09:31
    关注

    Code :

    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $("#update-details-btn").on('click', function() {
            alert("inside");
            $.ajax({
                type: 'post',
                dataType: 'json',
                url: 'php_file.php',
                success: function(data)
                {
                    console.log(data);
                    $('div#update_details_body').html(data.results);
                }
            });
        });
    });
    </script>
    </head>
    <style>
    .modal-body
    {
        margin: 0px;
        padding: 0px;
        width: 100px;
        height: 100px;
    }
    </style>
    <body>
    <div id="update_details_body" class="modal-body"></div>
    <input type="button" id="update-details-btn" name="button" value="button">
    </body>
    </html>
    

    php code not chnage

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统
  • ¥15 关于wkernell.PDB加载的问题,如何解决?(语言-c#|开发工具-vscode)
  • ¥100 某宝多次访问被拒绝,求解
  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!