2017-07-07 30 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 + ','); 
}); 

入力フィールドでそれらを移入するには、なぜデータ・プロダクト・コードの値は、データ - と同じです商品名?私はそれを解決することができませんでした、なぜなら、1要素は2つのデータ属性を含むことができないからですか?

+0

あなたは完全なHTMLを示すことができる:ここで

は修正されたコードがありますか? – JiFus

答えて

1

これはどちらの場合でもdata関数を呼び出す代わりにtext関数を呼び出すためです。

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

ありがとうADreNaLiNe-DJ、ちょうど私の日を救った。 –

関連する問題