Jquery 无法使用mydata构建JSON

Jquery 无法使用mydata构建JSON,jquery,Jquery,我的屏幕是这样的 屏幕中有2个项目 对于项目1==>选择蜂蜜加黄油 对于第2项==>坚果和干果,选择10克 基于此,我尝试构建下面的JSON,如图所示 [ { "name": "Item 1", "value": [ "None", "Honey with Butter" ] }, { "name": "Item 2", "value": [

我的屏幕是这样的

屏幕中有2个项目

对于项目1==>选择蜂蜜加黄油

对于第2项==>坚果和干果,选择10克

基于此,我尝试构建下面的JSON,如图所示

[
    {
        "name": "Item 1",
        "value": [
            "None",
            "Honey with Butter"
        ]
    },
    {
        "name": "Item 2",
        "value": [
            "Nutts and dry fruits10 gm",
            "None"
        ]
    }
]
我试过这样做

var toppings = [];



 $('.tdHeading').each(function () {
                values = [];
                $(this).each(function () {
                    if($(this).hasClass('tpActive'))
                    {

                        alert($(this).attr('topp_name'))
                         values.push($(this).attr('topp_name'));
                    }
                    else
                    {
                         values.push('None');
                    }

                });
                if(values.length>0)
                {
                    toppings.push({
                        'name': $(this).text().trim(),
                        'value': values
                    });
                }
            });


console.log(JSON.stringify(toppings));
但我得到的是下面的订单

[
    {
        "name": "Item  1",
        "value": [
            "None"
        ]
    },
    {
        "name": "Item  2",
        "value": [
            "None"
        ]
    }
] 
请帮助解决此问题


要使其正常工作,需要采用列表形式:

<ul class="tdHeading">
  <li class="name">Item 1</li>
  <li class="value tpActive">...</li>
</ul>

您正在寻找h6标题的子项。没有。所有部分都是彼此的兄弟部分。i、 e

<aside>
    <h6>A heading with no children</h6>
    <section><a>At the same hierarchical as the heading and the next section</a></section>
    <section><a>ditto</a></section>
</aside>
...


然后使用find来处理h6和锚定子元素。

您需要查找同级子元素:

  $(this).siblings().each(function () {
                    if($(this).children().hasClass('tpActive'))
                    {
                        alert($(this).children('.tpActive').attr('topp_name'))
                         values.push($(this).children('.tpActive').attr('topp_name'));
                    }

无类
tActive
在标记中为麻烦起见,我无权修改CSS,因为它是由其他人编写的,我如何才能使它与Fiddle中的一样工作感谢你发现了我的错误。但是为什么没有一个不出现(我指的是其他条件。hasClass('tActive'))那是个错误。我以为你不想要不匹配的条目。
  $(this).siblings().each(function () {
                    if($(this).children().hasClass('tpActive'))
                    {
                        alert($(this).children('.tpActive').attr('topp_name'))
                         values.push($(this).children('.tpActive').attr('topp_name'));
                    }