weixin_33699914 2016-01-16 19:55 采纳率: 0%
浏览 33

JSON结果重复

I have made a simple script to call JSON file and show on the website.

Unfortunately it shows double of the result. The problem is caused by the pictures. If I remove them the text is shown only once as I want it.

Here is the script

$(function () {

$('#members p, #members p span, #main_content span').on('click', function () {
    var attr = $(this).attr('data-number');
    if (typeof attr !== typeof undefined && attr !== false) {
        var number = $(this).data('number');
        console.log(number);
        $.ajax("members.json", {
            dataType: 'json',
            success: function (response) {

                $(".images_ajax").css('display', 'block');
                $(".images_ajax_sub").append(response[number].img + response[number].text);
            }
        }),
        $(function () {
            $('.close_ajax').click(function () {
                $(".images_ajax_sub h1, .images_ajax_sub p, .images_ajax_sub .iMembers").remove();
                $(".images_ajax").css('display', 'none');
            })
        })
    }
})});

If I remove the image from the JSON file then I get only one undefined as the image is missing not two.

Here is the part of the JSON file

"name": {
"img": "<div class=\"iMembers\"><img src = \"../images/images/images_of_members/some.jpg\"></div>",
"text": "<p>some text</p>"
}
  • 写回答

1条回答 默认 最新

  • weixin_33698043 2016-01-16 20:25
    关注

    My best guess is that your AJAX call gets fired twice. Take a look at the DOM nodes that the click handler is being bound to:

    $('#members p, #members p span, #main_content span').on('click ...
       ^^^^^^^^^^  ^^^^^^^^^^^^^^^
    

    Since you don't prevent event bubbling, a click on a span triggers the bound event handler, then bubbles up the DOM and triggers again for the span's parent node: p. Thus, try binding to either or.

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘