2012-03-14 8 views
1

複数のdiv( "show" + i)に同時に機能を割り当てたい場合、それぞれの呼び出しは別のdiv( "theDiv" + i)、 "+ i"は機能しません。

$("#show"+i).click(function() 
{ 
$("#theDiv"+i).show("normal"); 
}); 

"i"の値(0,1,2 ...)を再配置すると、コードは機能しています。では、なぜこれを修正できますか? これは全体の機能である:

function getInfoProduits1() 
{ 

    var request = new XMLHttpRequest(); 
    if(i<idProduits.length) 
     { 
      request.open("GET","http://patisserie-orient.fr/prestashop/prestashop/api/products/"+idProduits[i]+"?PHP_AUTH_USER="+PHP_AUTH_USER+"&ws_key="+ws_key,true); 

      request.onreadystatechange = function() 
       { 
        if(request.readyState==4) 
        { 
         //alert("Status2 is "+request.status); 
         if (request.status == 200 || request.status == 0) 
         { 
          response1 = request.responseXML.documentElement; 
          nameProduits[i] = response1.getElementsByTagName('name')[0].getElementsByTagName('language')[0].firstChild.data; 
          //alert(nameProduits[i]); 
          priceProduits[i] = response1.getElementsByTagName('price')[0].firstChild.data+" €"; 
          //alert(priceProduits[i]); 
          descriptionProduits[i] = response1.getElementsByTagName('description_short')[0].getElementsByTagName('language')[0].firstChild.data; 
          //alert(descriptionProduits[i]); 
          quantitieProduitsDisponible[i] = response1.getElementsByTagName('quantity')[0].firstChild.data; 
          //alert(quantitieProduitsDisponible[i]); 

          maDiv = document.createElement("div"); 
          maDiv.id = 'id_de_la_div'+i; 

          malabel = document.createElement("div"); 
          malabel1 = document.createElement("div"); 
          mon_bouton1 = document.createElement("button"); 
          maDiv2 = document.createElement("div"); 
          mon_bouton1.id="show"+i; 
          maDiv2.id="theDiv"+i; 
          maDiv2.innerHTML="Détails:"; 
          maDiv2.style.display="none"; 
          //mon_bouton1.value='click'; 

          $("#show"+i).click(function(){ 

      $("#theDiv"+i).show("normal"); 
      }); 
         mon_bouton1.onclick='aff();'; 
          malabel.innerHTML=nameProduits[i]; 
          malabel1.innerHTML=priceProduits[i]; 
          maDiv.innerHTML='<img src="Images/Icones/dollars.png" width="50" height="50" align="right">'; 
          //maDiv.innerHTML='<button type="button" id="show" onclick="aff();">Détails</button> '; 
          maDiv.className="ui-bar ui-bar-e cadre"; 
          document.getElementById("divbar").appendChild(maDiv); 
          document.getElementById('id_de_la_div'+i).appendChild(malabel); 
          document.getElementById('id_de_la_div'+i).appendChild(malabel1); 
          document.getElementById('id_de_la_div'+i).appendChild(mon_bouton1); 
          document.getElementById('id_de_la_div'+i).appendChild(maDiv2); 
          maDivvide = document.createElement("div"); 
          maDivvide.innerHTML='<br>'; 
          document.getElementById("divbar").appendChild(maDivvide); 
          i++; 
          getInfoProduits1(); 
         } 
        } 
       } 

      request.send(); 

     } 
    else 
     { 
      return; 
     } 

} 
+3

どのように定義されていますか?関連するコードを投稿できますか? – j08691

+0

機能の詳細を表示してください私たちは 'i'の初期化を検証することができますので。 – Fourth

+0

初期化して値を 'i'に割り当てるコードを投稿できますか? –

答えて

3

私はあなたが本当に何を意味するのか理解していないが、多分これはあなたが私はあなたが古典的な失策の1の犠牲に落ちたんだと信じて

//attach an event handler to all elements with an id that starts with "show" 
$("[id^=show]").click(function(){ 
    //get only the number from the clicked id 
    var i = this.id.replace("show", ""); 
    //show the relevant div 
    $("#theDiv"+i).show("normal"); 
}); 
+0

+1、それは素敵なトリックです – JaredPar

+0

素晴らしい解決策! 7amdoulellah !!ありがとう@ニコラペルチェッティ –

0

を必要とするものです死に至るときにシチリア人に立ち向かうことは決して有名ではない。

閉包で使用されているがその外側で定義された変数は、参照によって渡されます - 誰もが「i」を共有します。その関数を呼び出すときはいつでも、私はそれが割り当てられた最後の値と同じで、閉じたときに設定された値ではありません。これを修正するには、関数を使って 'i'を実行して閉じます。関数は値渡しであるため、現在の値 'i'をクロージャにバインドします。

function gimmeAFunctionToDoThatThingThatNeedsDoing(i) { 
    return function() { 
     // Move closure here 
    }; 
} 
関連する問題