• <legend id='iz87z'><style id='iz87z'><dir id='iz87z'><q id='iz87z'></q></dir></style></legend>

        <bdo id='iz87z'></bdo><ul id='iz87z'></ul>
      <i id='iz87z'><tr id='iz87z'><dt id='iz87z'><q id='iz87z'><span id='iz87z'><b id='iz87z'><form id='iz87z'><ins id='iz87z'></ins><ul id='iz87z'></ul><sub id='iz87z'></sub></form><legend id='iz87z'></legend><bdo id='iz87z'><pre id='iz87z'><center id='iz87z'></center></pre></bdo></b><th id='iz87z'></th></span></q></dt></tr></i><div id='iz87z'><tfoot id='iz87z'></tfoot><dl id='iz87z'><fieldset id='iz87z'></fieldset></dl></div>
    1. <tfoot id='iz87z'></tfoot>

      1. <small id='iz87z'></small><noframes id='iz87z'>

      2. Jquery 循环无法正常工作?

        时间:2023-09-20
          • <bdo id='ABrDX'></bdo><ul id='ABrDX'></ul>
            1. <small id='ABrDX'></small><noframes id='ABrDX'>

              <tfoot id='ABrDX'></tfoot>
            2. <legend id='ABrDX'><style id='ABrDX'><dir id='ABrDX'><q id='ABrDX'></q></dir></style></legend>

                <tbody id='ABrDX'></tbody>
                <i id='ABrDX'><tr id='ABrDX'><dt id='ABrDX'><q id='ABrDX'><span id='ABrDX'><b id='ABrDX'><form id='ABrDX'><ins id='ABrDX'></ins><ul id='ABrDX'></ul><sub id='ABrDX'></sub></form><legend id='ABrDX'></legend><bdo id='ABrDX'><pre id='ABrDX'><center id='ABrDX'></center></pre></bdo></b><th id='ABrDX'></th></span></q></dt></tr></i><div id='ABrDX'><tfoot id='ABrDX'></tfoot><dl id='ABrDX'><fieldset id='ABrDX'></fieldset></dl></div>
                • 本文介绍了Jquery 循环无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试创建一个循环来创建许多函数,以便当用户单击拇指向上按钮时,它会运行正确的 .php 文档.当我删除循环并只给 var i 一个特定的数字时,它工作得很好,但是一旦我尝试将它变成一个循环,在 alert(i) 我在第一个循环中得到 10.

                  I am trying to create a loop to create many functions so that when a user clicks the thumb up button it runs the correct .php document. It works great when I remove the loop and just give var i a specific number but as soon as i try to make it into a loop, at the alert(i) i get 10 on the first loop.

                   var i=1;
                   while ( ++i < 10 ) {
                      $('#thumbup' + i).click(function() {
                          var userid = $('#theuser' + i).text();
                          var url = "_thumbup.php?userid=" + userid;
                          //alert(url);
                  
                          $('#thumbup' + i).hide();
                          $('#thumbdown' + i).hide();
                  
                          $("#toggle").css("display","block");
                          alert(i); // Give me 10 on first loop?!?
                  
                          // get the URL
                          http = new XMLHttpRequest(); 
                          http.open("GET", url, true);
                          http.send(null);
                  
                          // prevent form from submitting
                          return false;   
                  
                      }); 
                    }
                  

                  推荐答案

                  这是一个经典问题:当你的回调被调用时,i 的值是 end of loop.

                  This is a classical problem : by the time your callbacks are called, i has the value of end of loop.

                  解决方法如下:

                  var i=1;
                  while ( ++i < 10 ) {
                     (function(i){
                        // your current code
                     })(i);
                  }
                  

                  之所以有效,是因为内部函数在调用时创建了一个作用域,而这个作用域包含了你想要的i的值.

                  It works because the internal function creates a scope when it is called, and this scope contains the value of i you want.

                  这篇关于Jquery 循环无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:每三个 div 的 php while 循环变量 下一篇:PHP While 循环只显示最后一行

                  相关文章

                  最新文章

                    <bdo id='JIhBJ'></bdo><ul id='JIhBJ'></ul>
                    <legend id='JIhBJ'><style id='JIhBJ'><dir id='JIhBJ'><q id='JIhBJ'></q></dir></style></legend>
                  1. <i id='JIhBJ'><tr id='JIhBJ'><dt id='JIhBJ'><q id='JIhBJ'><span id='JIhBJ'><b id='JIhBJ'><form id='JIhBJ'><ins id='JIhBJ'></ins><ul id='JIhBJ'></ul><sub id='JIhBJ'></sub></form><legend id='JIhBJ'></legend><bdo id='JIhBJ'><pre id='JIhBJ'><center id='JIhBJ'></center></pre></bdo></b><th id='JIhBJ'></th></span></q></dt></tr></i><div id='JIhBJ'><tfoot id='JIhBJ'></tfoot><dl id='JIhBJ'><fieldset id='JIhBJ'></fieldset></dl></div>

                  2. <small id='JIhBJ'></small><noframes id='JIhBJ'>

                    1. <tfoot id='JIhBJ'></tfoot>