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
5 Answers
Reset to default 2To 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
版权声明:本文标题:javascript - JQuery for loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743948225a2566803.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论