我的屏幕朝这边看
屏幕上有两个项目。
对于第1项,选择了带有黄油的===>蜜
对于第2项,选择了===>螺母和干fruits10 gm。
基于此,我尝试在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"
]
}
]
请帮助解决这个问题。
http://jsfiddle.net/7xdqvaay/1/
发布于 2014-09-06 19:58:24
你需要寻找兄弟姐妹,孩子:
$(this).siblings().each(function () {
if($(this).children().hasClass('tpActive'))
{
alert($(this).children('.tpActive').attr('topp_name'))
values.push($(this).children('.tpActive').attr('topp_name'));
}
http://jsfiddle.net/7xdqvaay/12/
发布于 2014-09-06 19:49:57
您正在寻找您的h6标题的子标题。根本就没有。所有的部分都是彼此的兄弟姐妹。即
<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>
...
您应该迭代每个$('aside')
元素来修复这个问题。例如http://jsfiddle.net/7xdqvaay/11/
然后使用find处理h6和锚子元素。
发布于 2014-09-06 19:38:05
要使其工作,它需要以列表形式出现:
<ul class="tdHeading">
<li class="name">Item 1</li>
<li class="value tpActive">...</li>
</ul>
看看你是怎么循环的,我建议:
$('.tdHeading').each(function () {
$(this).find('.value').each(function () { ... });
...
});
https://stackoverflow.com/questions/25703898
复制相似问题