2015-12-17 4 views
5

私は、同じクラス名とカスタム属性を持つ約1000個の画像とテキストエリアを持っています。クラス名はそれぞれemoticonとemoticonlistです。カスタム属性はemo-tagとemo-as​​ciiです。異なるHTML属性の項目を配列に取り込む

各画像には、そのカスタム属性にまったく同じ内容のパートナー(テキストエリア)があります。

例:999

に、xは0から番号を表し

emo-tag = "f-x" // for images 
emo-ascii = "f-x" // for textareas 

私のスクリプトは、画像の属性と私は問題なく必要をキャプチャします。問題は、イメージのような正確な属性の内容を持つtextareaの値を取得しようとすると開始されます。私が言ったように、コードは動作しますが、キャプチャされ、配列の中に押し込まテキストエリアがちょうど最初の1、配列のすべてのアイテムが

$(function(){ 
var json = []; 

$('img').each(function(){ 
    var emoimg = $(this).attr("src"); 
    var emoalt = $(this).attr("alt"); 
    var emotag = $(this).attr("emo-tag"); 

    //Does not this supposed to capture the value of this specific textarea? 
    var emoascii= $('.emoticonlist').attr("emo-ascii",emotag).val(); 

     json.push({ 
      id : emotag, 
      name : emoalt, 
      img : emoimg, 
      content: emoascii  
     }); 

}); 
    var s = JSON.stringify(json); 
    $("#content").after("<div>" + s + "</div>"); 
}); 

です:

は、ここに私のコードです。私は何をしたいのですか?

電流出力:

[ 
{"id":"emo-0","name":"Smiley Face","img":"images/smiley-face.png","content":":)"}, 
{"id":"emo-1","name":"Big smile","img":"images/big-smile.png","content":":)"}, 
{"id":"emo-2","name":"Sad face","img":"images/sad-face.png","content":":)"}, 
... 
... 
... 
] 

所望の出力:

[ 
{"id":"emo-0","name":"Smiley Face","img":"images/smiley-face.png","content":":)"}, 
{"id":"emo-1","name":"Big smile","img":"images/big-smile.png","content":":D"}, 
{"id":"emo-2","name":"Sad face","img":"images/sad-face.png","content":":("}, 
... 
... 
... 
] 
+0

は、あなたが作業フィドルの例を与えることを編集する 'コード・ブロック' ツールを使用してもらえてライン

var emoascii= $('.emoticonlist').attr("emo-ascii",emotag).val(); 

を交換してみてください? cantがうまく動作しない場合は、十分なhtmlを含めて、簡単なフィドルを作ることができるようにしてください。感謝 –

+0

'$( '。emoticonlist')。attr(" emo-as​​cii "、emotag)'は属性を設定します。それはあなたが好きなようにそれを得ることはありません(http://api.jquery.com/attr/)。 – Lucas

答えて

関連する問題