开发者

increment the i

for(var i=1;i<=($("input:regex(id,[0-9]+)").length);i++)
{
    array.push($("#i").val());
} 

Hey, I am tying to store the input value in the array. Each of the input has distinct id associated with it, in this case, 1,2,3,4 and so on. How can I change the i in

array.push($("#i").val());

accordingly with the counter i in the for loop, which would fetch the value from inputs.开发者_如何学C like if I got two inputs fields, whose ids are 1 and 2. After this codes execution, the array has two elements that are from the inputs. Thank you


This should do, just use a + sign to concat the string of "#" with the number from i

for(var i=1;i<=($("input:regex(id,[0-9]+)").length);i++)
{
    array.push($("#"+i).val());
}


Just use the string concatenation, or + operator:

array.push($("#" + i).val());


How can you change the i in the expression? Do you mean like array.push($("#"+i).val());?


$('input[id]').each(function() {
    array.push($(this).val());
});

Hope I got it right because It is hard to understand your question!


You could map the values instead of looping over each one of them.

var array = $("input:regex(id,[0-9]+)").map(function() { 
    return this.value;
}).get();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜