2017-07-07 87 views
0

我把在多个元素(数据产品名称&数据产品代码),其目的是填充在一些输入字段从数据属性值2相同的数据属性。jQuery的显示值相同的属性

<strong class="displayed_product_name" data-product-name="All content" data-product-code="abc">All content</strong> 

我抓住数据属性值,并将它们作为数组。

var arrProductCode = []; 
$(this).closest('.each_content_item').find('[data-product-code]').each(function() { 
    arrProductCode.push($(this).text()); 
}); 

var arrProductName = []; 
$(this).closest('.each_content_item').find('[data-product-name]').each(function() { 
    arrProductName.push($(this).text()); 
}); 

填充它们在元件和输入字段

$(arrProductName).each(function() { 
    $('.pop_edit_country').find('.pop_edit_product_list ul').append("<li>" + this + "</li>"); 
}); 
$(arrProductCode).each(function() { 
    $('.hidden_edit_country_product_list').val($('.hidden_edit_country_product_list').val() + arrProductCode + ','); 
}); 

数据产品名称的值正确地抓住,但为什么的数据积代码的值是相同的DATA-产品名称?我无法解决它,是因为1个元素不能包含2个数据属性?

+0

你能不能显示完整的HTML? – JiFus

回答

1

这是因为在这两种情况下,你叫text函数而不是调用data功能。

text函数获取元素的文本上下文,而data函数获取您传递给它的data attribute的内容。

以下是更正代码:

var arrProductCode = []; 
$(this).closest('.each_content_item').find('[data-product-code]').each(function() { 
    arrProductCode.push($(this).data('product-code')); 
}); 

var arrProductName = []; 
$(this).closest('.each_content_item').find('[data-product-name]').each(function() { 
    arrProductName.push($(this).data('product-name')); 
}); 
+0

由于肾上腺素DJ,你刚才救了我的天。 –