2012-05-04 10 views
0

これは簡単な解決策になりますが、私はそれを自分で解決できないようです。JQuery .each()関数で苦労して

背景には、私が

など、あなたが友人の選択が終了したときにはjQuery UIのオートコンプリートとその壁にポストにすべてのFacebookの友達を置くWebアプリケーションを作成してリンク/ボタンをクリックしようとしていますです

私は1人の友人にとってこれはうまく動作しましたが、複数の友人にこの仕事をさせるための要件は(今までどおり)変更されました。

これまでのところ、私は実際にはユーザーの友人の壁に投稿する場合を除いて、複数の友達と仕事をしています。

私は、IDSを取り込むために管理する非表示の入力を持っている:私は、私は次のようであると考えての質問に関連するコードにこれを維持しようとする

コード

<input id="fbid" type="hidden" value="12345, 567890, "> 

次に、友だちの壁に投稿するためにリンクをクリックしたときに実行されるjQuery機能があります。

<div id="fb-root"></div> 
<script> 
window.fbAsyncInit = function() { 
    FB.init({ 
     appId  : '12345678', // App ID 
     channelUrl : '/channel.html', // Channel File 
     status  : true, // check login status 
     cookie  : true, // enable cookies to allow the server to access the session 
     oauth  : true, // enable OAuth 2.0 
     xfbml  : true // parse XFBML 
    }); 

    // Additional initialization code here 

    FB.login(function(response) 
    { 
     if (response.authResponse) 
     {            
      $("#send-voucher").click(function() { 

       // Post message to friend's wall 

       var opts = { 
        message : 'This is the message', 
        name : 'This is the name', 
        link : 'http://www.opticalexpress.co.uk', 
        description : 'This is the description', 
        picture : '/voucher_valid.png' 
       }; 

       var referees = $("#fbid").val(); 

       // remove last comma and space from the end of the string 
       referees = $.trim(referees); 
       referees = referees.substring(0, referees.length - 1); 

       var referee = referees.split(', '); // comma space 
       referee.each(function() { 

        FB.api('/'+ referee +'/feed', 'post', opts, function(response) 
        { 
         if (!response || response.error) 
         { 
          alert('Posting error occured'); 
         } 
         else 
         { 
          alert('Success - Post ID: ' + response.id); 
          $("#send-voucher").hide(); 
          $("#voucher-sent").fadeIn('slow'); 
         } 
        }); 
       }); 
      }); 
     } 
     else 
     { 
      alert('Not logged in'); 
     } 
    }, { scope : 'publish_stream' }); 
}; 

放火犯によって報告されたエラーは、任意のより詳細な情報が必要な場合は、ちょうど私に教えてくださいreferee.each is not a function

です。

答えて

2

それはjQueryのラッパーを必要とする、これを変更:

referee.each(function() { 

へ:

$(referee).each(function() { 
+0

ああ...ありがとうございます。 Facebookの部分と関係があると思われる新しいエラー。ありがとう! – martincarlin87

+0

あなたの質問に@martincarlin87;と答えることを忘れないでください) – nyson

+0

@nyson - 8分待たなければなりませんでした。エラーは現在、文字列が実際に分割されていないようです......それは '12345、' 567890'の代わりに '12345、567890'に2回ポストしようとします...... – martincarlin87

1

あなたはjQueryオブジェクトであるとしてあなたが唯一のeach()を使用することができます。あなたのreferees変数は、文字列の配列ですので、あなたは基本$ jQueryオブジェクトからそれを呼び出すと、このように、パラメータとして渡す必要があります。

$.each(referee, function() { 
    // ... 
} 

別の方法としては以下のように、jQueryオブジェクトであなたの変数をラップすることができますこの:

$(referee).each(function() { 
    // ... 
} 
1

あなたはこのようにそれを呼び出す必要があり:

$.each(referee, function() { ... }); 

jQuery.each()その後、最初のパラメータcallbとしてコレクションを必要としますack機能。