admin管理员组

文章数量:1356789

I need to Loop in JQuery from 0 to variable-value(dynamically entered by user).How can i achieve this? Now i am doing it by using simple For loop like this.

for( i=1; i<=fetch; i++) {  
   var dyndivtext = document.createElement("input");
   document.body.appendChild(dyndivtext); 
}

Thanks.

I need to Loop in JQuery from 0 to variable-value(dynamically entered by user).How can i achieve this? Now i am doing it by using simple For loop like this.

for( i=1; i<=fetch; i++) {  
   var dyndivtext = document.createElement("input");
   document.body.appendChild(dyndivtext); 
}

Thanks.

Share Improve this question edited Aug 24, 2012 at 8:10 Ram 145k16 gold badges172 silver badges200 bronze badges asked Aug 24, 2012 at 8:06 user853575user853575 1213 gold badges4 silver badges12 bronze badges 8
  • How does users entered the variable? – fankt Commented Aug 24, 2012 at 8:08
  • You could still use foo loop in jQuery. – xdazz Commented Aug 24, 2012 at 8:09
  • 2 What's wrong with your current code? – Alexei Levenkov Commented Aug 24, 2012 at 8:10
  • It is working in javascript for loop now.i need to do it in JQuery using .each loop. – user853575 Commented Aug 24, 2012 at 8:15
  • @evanc3 User enter the variable in a text box that i am retrieving in a variable named "fetch". – user853575 Commented Aug 24, 2012 at 8:16
 |  Show 3 more ments

5 Answers 5

Reset to default 2

To loop between two values you should use a regular Javascript loop. The jQuery each methods are used when looping through a collection of elements or an array.

To loop from zero, you should initialise the loop variable to zero, not one. To loop from zero to the specified value, you use the <= for the parison, but to loop from zero and the number of items as specified (i.e. from 0 to value-1), you use the < operator.

for (i = 0; i < fetch; i++) {
  $('body').append($('<input/>', { type: 'text' }));
}

You could loop an empty array:

$.each(new Array(fetch), function(i) { 
   var dyndivtext = document.createElement("input");
   document.body.appendChild(dyndivtext); 
});

If you do this alot you can even fake-patch jQuery.each to take numbers:

(function($) {
    var _each = $.each;
    $.each = function() {
        var args = $.makeArray(arguments);
        if ( args.length == 2 && typeof args[0] == 'number') {
            return _each.call(this, new Array(args[0]), args[1]);
        }
        return _each.call(this, args);
    };
}(jQuery));​

$.each(fetch, function(i) {
    // loop
});

jQuery.each does have some great features, like the different return values inside the callback. But for a simple loop I find it much more convenient (and less overhead) to do something like:

while(fetch--) {
    // loop
}​

You mean Javascript loop. From W3Schools:

for (var variable = startvalue; variable < endvalue; variable = variable + increment)
{
      //code to be executed
}

To get the value from user and run the code you can use the following prompt.

var x=prompt("Enter the value",0);
for(i=0;i<x;i++)
{
  var dyndivtext = document.createElement("input");
  document.body.appendChild(dyndivtext);
}

Hope this helps.

Thanks

If you want it the full jQuery way then use that new plugin jQuery-timing. It provides inline-loops in your jQuery line:

$('body').repeat().append('<input>').until(fetch);

Nice, eh?

本文标签: javascriptJQuery for loopStack Overflow