JSON解析元素上的属性重复项 [英] JSON parsed attributed duplicates on elements
本文介绍了JSON解析元素上的属性重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我认为我有一个复杂的问题.我通过jQuery.load()
从另一个页面加载表单,然后获取它的属性,通过JSON解析它并从中获取某些属性.这是JS:
I believe I have a complex problem. I load a form from another page via jQuery.load()
and then I'm taking it's attributes, parse it via JSON and getting certain attribute from it. Here's the JS:
function getFilter() {
var hash = window.location.hash;
if(hash.length) {
var hash2 = hash.replace('#',''),
exhash = hash2.split('&'),
getsize = exhash[0],
size = getsize.split('='),
getcat = exhash[1],
cat = getcat.split('=');
$('.prod').each(function() {
var link = $(this).attr('data-target');
$(this).append('<div class="field" style="display: none"></div>');
$(this).children('.field').load(link + ' form', function() {
var el = $(this).parent();
var form = $('.field').children('form').attr('data-product_variations'),
parse = $.parseJSON(form);
var searchTerm = size[1];
console.log(parse)
$.each(parse, function(i, item) {
console.log(item)
$.each(item, function(j, innerItem){
if(typeof(innerItem.attribute_pa_rozmiar) != "undefined"){ if(innerItem.attribute_pa_rozmiar == searchTerm)
{
console.log(el);
$(el).children('a').css('background','url('+parse[i].image_src+')');
$(el).children('a').animate({
opacity:1
},200)
}
}
});
});
});
})
}
};
我的问题是,即使我运行console.log
且值似乎不同,通常两个.prod
div都具有相同的background-image
.我该怎么办?
My problem is, very often both .prod
divs have the same background-image
, even if I run console.log
and values seem to be different. What can I do?
推荐答案
var form = $('.field').children('form').attr('data-product_variations'),
^我相信这是您的问题.您没有在当前'.prod'元素内指定带有class字段的div.
^ I believe this line is your problem. You're not specifying the div with class field inside the current '.prod' element.
尝试重写这些行:
$(this).append('<div class="field" style="display: none"></div>');
$(this).children('.field').load(link + ' form', function() {
var el = $(this).parent();
var form = $('.field').children('form').attr('data-product_variations'),
而是这样:
var field = $('<div class="field" style="display: none" />');
$(this).append(field);
field.load(link + ' form', function() {
var el = $(this).parent();
var form = field.children('form').attr('data-product_variations'),
这篇关于JSON解析元素上的属性重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文