简体   繁体   English

在包含相同ID的元素中输入JSON数据-故障排除

[英]Inputting JSON Data In Element's Containing Same ID - Troubleshooting

Currently I have JSON data that is being inputted/entered into all articles containing the ID #viewed . 目前,我有JSON数据正在输入/输入到所有包含ID #viewed文章中。 While the JSON data is showing that it is being inputted into all article's with the ID, the function checkViewers() is only functioning correctly for the first article ID #viewed . 尽管JSON数据显示已使用ID将其输入到所有文章中,但功能checkViewers()仅对第一个文章ID #viewed正确#viewed

Ideally, the checkViewers() function should make all #viewed IDs appear as the first article ID #viewed is currently appearing (eg First name Last name and Remaining Number of People have viewed this post.) However, the total number of people remaining is incorrect in the first article, as it is gathering all the repeated data. 理想情况下, checkViewers()函数应使所有#viewed ID出现在当前出现的#viewed的第一个文章ID上(例如,名字,姓氏和剩余人数已查看过该帖子。)但是,剩余的总人数是在第一篇文章中是不正确的,因为它正在收集所有重复的数据。 It should only be gathering the data once and totaling that number per article ID. 它应该只收集一次数据,并按商品ID总计该数字。

What is the best fix for this situation? 解决这种情况的最佳方法是什么? I am guessing the checkViewers() function is gathering all the data on the page and only needs to be gathering the data from it's parent section? 我猜想checkViewers()函数正在收集页面上的所有数据,而只需要从其父部分收集数据?

A sample of the current code: 当前代码示例:

  //Content Viewer Information
  function checkViewers() {
    //Base Variables
    //var viewer = $('#viewed span.user');
    //var totalViews = $('#viewed span.user').length;
    //var shortenViews = $('#viewed span.user').length -1;
    var viewer = $("#viewed span[class^='user']");
    var totalViews = $("#viewed span[class^='user']").length;
    var shortenViews = $("#viewed span[class^='user']").length -1;

    if (totalViews === 0) {
      ($('#viewed').html('<span> 0 people have viewed your post.</span>'));
    }
    if (totalViews === 1) {
      $('<span> has viewed your post.</span>').insertAfter(viewer.last());
    }
    if (totalViews === 2) {
      $('<span> and </span>').insertAfter(viewer.first());
      $('<span> have viewed your post.</span>').insertAfter(viewer.last());
    }
    if (totalViews >= 3) {
      viewer.slice(1).hide();
      $('<span> and </span>').insertAfter(viewer.first());
      $('<span class="user count"></span>').insertAfter(viewer.eq(2));
      $('.count').html(shortenViews + ' more people');
      $('<span> have viewed your post.</span>').insertAfter(viewer.last());
    }
  }

The function is then being called with the updated content. 然后使用更新的内容调用该函数。

  //Update Page With New Content
  var viewerSection = $("article[id^='viewed']");
  viewerSection.html(newViewers);
  checkViewers();

Edits: I ended up changing the IDs #viewed to the Class .viewed , as IDs should be unique. 编辑:我最终将ID #viewed更改为Class .viewed ,因为ID应该是唯一的。 However, I am still having the same problem as before. 但是,我仍然遇到与以前相同的问题。

View the current and complete code at Plunker . Plunker上查看当前代码和完整代码。

Firstly, now you are correctly using classes, you shouldn't check the class attribute like this: 首先,现在您正确使用了类,您不应该像这样检查class属性:

var viewerSection = $("div[class^='viewed']");

Just use the basic jQuery class selector: 只需使用基本的jQuery类选择器:

var viewerSection = $("div.viewed");

You should also do the same for the other 3 selectors at the top of your script as you have commented out. 注释掉后,还应该对脚本顶部的其他3个选择器执行相同的操作。


Now the main problem is that you are not restricting your checks in checkViewers to each individual item, so it is applying it globally. 现在的主要问题是您没有将checkViewers的检查限制为每个单独的项目,而是将其全局应用。

You need to loop through each .viewed element and apply your logic to each. 您需要遍历每个.viewed元素,并将您的逻辑应用于每个。 The jQuery selector method takes a second argument which is an element to search within for matches. jQuery选择器方法采用第二个参数,该参数是要在其中搜索匹配项的元素。 As you are in a jQuery each() method, you can just pass this as the second argument: 当您使用jQuery each()方法时,只需将this作为第二个参数传递即可:

  function checkViewers() {
    $('div.viewed').each(function() {
      var viewer = $("span.user", this);
      // no need to re-select, just work them out based on viewer
      var totalViews = viewer.length;
      var shortenViews = viewer.length -1;

      if (totalViews === 0) {
        $(this).html('<span>0 people have viewed your post.</span>');
      }
      else if (totalViews === 1) {
          $(this).append('<span> has viewed your post.</span>');
      }
      else if (totalViews === 2) {
        $('<span> and </span>').insertAfter(viewer.eq(0));
        $(this).append($('<span> have viewed your post.</span>'));
      }
      else if (totalViews >= 3) {
        viewer.slice(1).hide();
        $('<span> and </span>').insertAfter(viewer.eq(0));
        $('<span class="user count">' + shortenViews + ' more people</span>').insertAfter(viewer.eq(2));
        $(this).append($('<span> have viewed your post.</span>'));
      }
    });
  }

Updated Plunker 更新的柱塞

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM