我的屏幕看起来像这样
屏幕上有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));
但是我得到下面的O / P
[
{
"name": "Item 1",
"value": [
"None"
]
},
{
"name": "Item 2",
"value": [
"None"
]
}
]
请帮助解决此问题
您需要寻找兄弟姐妹的孩子:
$(this).siblings().each(function () {
if($(this).children().hasClass('tpActive'))
{
alert($(this).children('.tpActive').attr('topp_name'))
values.push($(this).children('.tpActive').attr('topp_name'));
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句