Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 变量";我";美元(“show”i)不起作用_Javascript_Jquery - Fatal编程技术网

Javascript 变量";我";美元(“show”i)不起作用

Javascript 变量";我";美元(“show”i)不起作用,javascript,jquery,Javascript,Jquery,我想同时给几个div分配一个函数(“show”+I),每次调用都会分配另一个div(“theDiv”+I),但“+I”不起作用 $("#show"+i).click(function() { $("#theDiv"+i).show("normal"); }); 如果我用它的值(0,1,2…)重新放置“I”,代码将正常工作。那么我为什么要纠正这个问题呢? 这是整个功能: function getInfoProduits1() { var request = new XMLHttpReq

我想同时给几个div分配一个函数(“show”+I),每次调用都会分配另一个div(“theDiv”+I),但“+I”不起作用

$("#show"+i).click(function()
{
$("#theDiv"+i).show("normal");
});
如果我用它的值(0,1,2…)重新放置“I”,代码将正常工作。那么我为什么要纠正这个问题呢? 这是整个功能:

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;
        }

}
函数getInfoProduits1() { var request=new XMLHttpRequest();
如果(我我不明白你真正的意思,但也许这就是你需要的

//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");
});

我相信你已经成为一个经典错误的受害者,只是比“当死亡迫在眉睫时,千万不要与西西里人对抗”稍微逊色一点

在闭包中使用但在闭包外部定义的变量是通过引用传递的-每个人都共享“i”。因此,无论何时调用该函数,您都会发现“i”始终等于分配给它的最后一个值,而不是构建闭包时设置的值。您可以通过在函数中运行“i”来修复此问题,从而为您提供闭包。因为函数是按值传递的,您将获得绑定到闭包的当前值'i'

function gimmeAFunctionToDoThatThingThatNeedsDoing(i) {
    return function() {
       // Move closure here
    };
}

我是如何定义的?你能发布相关的代码吗?请展示更多的函数,这样我们就可以验证“i”的初始化。你能发布初始化的代码并将值赋给
i
?我愿意打赌,你可以重新编写它来使用类,而不是迭代一堆编号的ID。或者单击时显示“show”元素的索引,或者如果“theDiv”使用DOM遍历具有一致的关系(第二个嵌套div或其他)。如果我理解您更新的问题,您正在尝试进行模式匹配。但是“+I”不会这样做。“I”表示应该定义的内容。例如,如果在函数之前您只编写了
var i=1;
,那么它将适用于
\show1
\theDiv1
。查看Nicola的答案。非常好的解决方案!!7Amdoulelah!!谢谢@Nicola Peluchetti