doutuo2829 2016-01-18 06:44
浏览 47
已采纳

jQuery JSON解析并在不同的位置附加数据

I have a page and I want to show and refresh different ads on different locations. I'm getting response from PHP File successfully via AJAX but unable to append them to specific placements.

HTML CODE

<script>var ads = [];</script>
<div id="ad-728x90">fhhfgh</div>

<!-- Page Content -->
<div class="container">

    <!-- Heading Row -->
    <div class="row">
        <div class="col-md-8">
            <img class="img-responsive img-rounded" src="http://placehold.it/900x350" alt="">
        </div>
        <!-- /.col-md-8 -->
        <div class="col-md-4">
            <h1>Business Name or Tagline</h1>
            <p>This is a template that is great for small businesses. It doesn't have too much fancy flare to it, but it makes a great use of the standard Bootstrap core components. Feel free to use this template for any project you want!</p>
            <a class="btn btn-primary btn-lg" href="#">Call to Action!</a>
        </div>
        <!-- /.col-md-4 -->
    </div>
    <!-- /.row -->

    <hr>

    <!-- Call to Action Well -->
    <div class="row">
        <div class="col-lg-12">
            <div class="well text-center">
                This is a well that is a great spot for a business tagline or phone number for easy access!
            </div>

            <div id="ad-300x250" class="ads"></div>
        </div>
        <!-- /.col-lg-12 -->
    </div>
    <!-- /.row -->

Javascript / jQuery code

<script type="text/javascript">ads.push("728x90", "300x250");</script>
<script>
$(document).ready(function()
{
    function loadAds()
    {
        $.post('ads.php', { adID: ads }, function (e) {

            if (e.status == 'error')
            {
                $('.ads').each(function ()
                {
                    $(this).remove();
                });
            }
            else if (e.status == 'ok')
            {
                var data = e.data;
                $.each(data, function (adID)
                {
                    $('#ad-' + adID).find('.ads').html();
                });
            }
        }, 'json');
    }

    loadAds();

});
</script>

JSON Response:

{"status":"ok","data":{"728x90":" \t \t\t \t\t\t

728x90 ads</p> \t\t</div> \t</div>","300x250":" \t \t\t \t\t\t

300x250 ads</p> \t\t</div> \t</div>"}}

  • 写回答

1条回答 默认 最新

  • dongzhankou2090 2016-01-18 06:54
    关注

    The following should place data in proper id

    $.each(data, function (adID, adHtml){
          $('#ad-' + adID).html(adHtml);
    });
    

    The html shown in question doesn't show any children with class ads in <div id="ad-728x90"> so find('.ads') was removed

    Question needs clarification of expected results if this doesn't work

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog