javascript - json:当单击类别链接时,我只想显示该类别中的帖子

标签 javascript jquery json

我想要获取分配给每个类别的帖子列表,并显示带有 slug 作为链接的标题,我不想对类别名称进行硬编码,而是想获取分配给每个类别的每个帖子。我希望这是有道理的。

我的问题是,如何比较该帖子是否属于“all-qualified-retirement-plans”类别的一部分?

我想做的是......当我点击链接“#all-qualified-retirement-plans”时,我只希望这些帖子以类别“all-qualified-retirement-plans”显示

    <ul data-role="listview" data-inset="true" class="ui-listview ui-listview-inset ui-corner-all ui-shadow">

    <li data-id="4" data-parent="1" class="ui-first-child">
<a href="#all-qualified-retirement-plans" class="ui-btn ui-btn-icon-right ui-icon-carat-r">All Qualified Retirement Plans</a></li>

  <li data-id="7" data-parent="0" class="ui-first-child">
<a href="#fica-ss-medicare-taxes" class="ui-btn ui-btn-icon-right ui-icon-carat-r">FICA - SS &amp; MEDICARE TAXES</a></li>

  <li data-id="2" data-parent="0">
<a href="#healthcare-plans" class="ui-btn ui-btn-icon-right ui-icon-carat-r"/> Healthcare Plans </a></li>

      <li data-id="3" data-parent="0">
<a href="#other-benefits" class="ui-btn ui-btn-icon-right ui-icon-carat-r">Other Benefits</a></li>

        </ul>


function allQualifiedRetirementPlans(data) {
  "use strict";

  var output = '<ul  data-role="listview" data-filter="true" data-filter-placeholder="Search..." >';

  $.each(data.posts, function(key, val) {
    output += '<li>';
    output += '<a href="#' + val.slug + '">';  // post slug
    output += '<h3>' + val.title + "</h3>";  // post title
    output += '</a>';
    output += '</li>';
  }); //go through each post


  output += "</ul>";
  $('#all-qualified-retirement-plans-postlist').html(output);

//json

var data = {
      "status": "ok",
        "count": 24,
        "count_total": 24,
        "pages": 1,
        "posts": [
            {
                "id": 131,
                "type": "benefits",
                "slug": "highly-compensated-employee-hce",
                "url": "http://mywebsite.com/benefits/highly-compensated-employee-hce/",
                "status": "publish",
                "title": "Highly Compensated Employee (“HCE”)",
                "title_plain": "Highly Compensated Employee (“HCE”)",
                "content": "<p>Qualified Retirement Plans must not favor Highly Compensated Employees (HCEs). HCEs are used in testing for discrimination, such as testing employee and employer contributions under a 401(k) plan. An HCE is one who either: a) owns more than 5% of the organization, either directly or indirectly, no matter what that person’s pay is, or b) any employee whose pay from the organization, for the previous year, exceeds the amount shown below.</p>\n",
                "excerpt": "<p>Qualified Retirement Plans must not favor Highly Compensated Employees (HCEs). HCEs are used in testing for discrimination, such as testing employee and employer contributions under a 401(k) plan. An HCE is one who either: a) owns more than 5% of the organization, either directly or indirectly, no matter what that person’s pay is, or b) &hellip; <a href=\"http://mywebsite/benefits/highly-compensated-employee-hce/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Highly Compensated Employee (“HCE”)&#8221;</span></a></p>\n",
                "date": "2016-04-01 18:06:34",
                "modified": "2016-04-01 18:06:54",
                "categories": [
                    {
                        "id": 4,
                        "slug": "all-qualified-retirement-plans",
                        "title": "All Qualified Retirement Plans",
                        "description": "",
                        "parent": 1,
                        "post_count": 4
                    },
                    {
                        "id": 1,
                        "slug": "retirement-plans",
                        "title": "Retirement Plans",
                        "description": "",
                        "parent": 0,
                        "post_count": 13
                    }
                ],
                "tags": [],
                "author": {
                    "id": 1,
                    "slug": "keith",
                    "name": "keith",
                    "first_name": "",
                    "last_name": "",
                    "nickname": "keith",
                    "url": "",
                    "description": ""
                },
                "comments": [],
                "attachments": [],
                "comment_count": 0,
                "comment_status": "closed",
                "custom_fields": {
                    "extra_content_extra_content_title": [
                        ""
                    ],
                    "extra_content_extra_content_medicare": [
                        ""
                    ],
                    "extra_content_extra_content_percent_moved": [
                        " "
                    ],
                    "extra_content_extra_content_percent_value": [
                        ""
                    ],
                    "extra_content_extra_content_first_value": [
                        "2016: 120,000"
                    ],
                    "extra_content_extra_content_second_value": [
                        "2015: 120,000"
                    ],
                    "extra_content_extra_content_third_value": [
                        "2014: 115,000"
                    ],
                    "extra_content_extra_content_fourth_value": [
                        "2013: 115,000"
                    ],
                    "extra_content_extra_content_firth_value": [
                        "2012: 115,000"
                    ],
                    "extra_content_extra_content_sixth_value": [
                        "2011: 110,000"
                    ],
                    "extra_content_extra_content_seventh_value": [
                        "2010: 110,000"
                    ],
                    "extra_content_extra_content_eight_value": [
                        ""
                    ],
                    "extra_content_extra_content_nine_value": [
                        ""
                    ],
                    "extra_content_extra_content_ten_value": [
                        ""
                    ],
                    "social_security_social_security_title": [
                        ""
                    ],
                    "social_security_social_security_field_one": [
                        ""
                    ],
                    "social_security_social_security_field_two": [
                        ""
                    ],
                    "social_security_social_security_field_three": [
                        ""
                    ],
                    "social_security_social_security_field_four": [
                        ""
                    ],
                    "social_security_social_security_field_five": [
                        ""
                    ],
                    "social_security_social_security_field_six": [
                        ""
                    ],
                    "social_security_social_security_field_seven": [
                        ""
                    ],
                    "social_security_social_security_field_eight": [
                        ""
                    ],
                    "social_security_social_security_field_nine": [
                        ""
                    ],
                    "social_security_social_security_field_ten": [
                        ""
                    ],
                    "extra_content_three_title": [
                        ""
                    ],
                    "extra_content_three_field_one": [
                        ""
                    ],
                    "extra_content_three_field_two": [
                        ""
                    ],
                    "extra_content_three_field_three": [
                        ""
                    ],
                    "extra_content_three_field_four": [
                        ""
                    ],
                    "extra_content_three_field_five": [
                        ""
                    ],
                    "extra_content_three_field_six": [
                        ""
                    ],
                    "extra_content_three_field_seven": [
                        ""
                    ],
                    "extra_content_three_field_eight": [
                        ""
                    ],
                    "extra_content_three_field_nine": [
                        ""
                    ],
                    "extra_content_three_field_ten": [
                        ""
                    ]
                }
            }
};

allQualifiedRetirementPlans (data)

最佳答案

看起来好像您缺少数据对象中 "posts" 字段中的关闭 ]

更新

我还包含了循环类别的逻辑,以查看对象数组中是否存在 slug。

此外,只是为了表明简单循环不需要 jQuery,我删除了 $.each() 方法

看这个 fiddle : https://jsfiddle.net/5yyg3req/2/

关于javascript - json:当单击类别链接时,我只想显示该类别中的帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36435640/

相关文章:

javascript - 带有选项(select)的jquery选择器抛出错误

javascript - $(element).append() 和 element.insertAdjacentHTML 有什么区别

javascript - 使用 jquery 或 javascript 将逗号分隔的键和值字符串转换为对象

javascript - 删除 html 类不会使用 jQuery removeClass 分离已删除的类功能

javascript - 具有 AJAX 和传统 POST 支持的 ASP.NET MVC 表单

javascript - 我不确定是否从 .js 脚本正确调用 .html 和 .json 文件

javascript - 导入组件自带的模块

json - 在golang中让节点树转json?

java - XmlAdapter 和 JSON 的 MOXy 异常

json - Golang json 解码 "unexpected end of JSON input"