开发者

How do I initialize an array in an input class for jquery?

var i;  
$('a.add').click(function() { 
    $('<p><input type="text" class = "count[i]" value="Enter Item Name"/></p>开发者_开发技巧;').animate({ opacity: "show" }, "slow").appendTo('#selection');
    itemNumber ++;
});

What I want to do is to initialize an array called count in the input class and have i as a counter. I'm not sure if this is the right way to go about this.


Check this jsfiddle and let me know whether this is what you want. In this case we have a array of class names and when a new item is added the class name is obtained from the array using counter i.

Here is my script

var i = 0;  

var count = ["class-1", "class-2", "class-3", "class-4"]

$('a.add').click(function(e) { 
    var className = i < count.length ? count[i] : "count";

    $('<p><input type="text" class = "'+ className  +'" value="Enter Item Name"/></p>').animate({ opacity: "show" }, "slow").appendTo('#selection');

    i++;
});

To access the count[i]th element you can use

$("#selection input." + count[i])

Or
You can check this fiddle In this the class names are incremented using the counter i whenever a new item is added.

Script

var i = 0;  

$('a.add').click(function(e) { 
    $('<p><input type="text" class = "class-'+ (++i)  +'" value="Enter Item Name"/></p>').animate({ opacity: "show" }, "slow").appendTo('#selection');
});

To access count[i]th element you can use

$("#selection input.class-" + i)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜