开发者

Getting the text of all inputs in a form using jquery

I am trying to consolidate all inputs on my form into one string, but my code just overwrites the var on each loop leaving me with only the text from the last input on t开发者_开发知识库he form... How can I fix this?

$(':input').each(function() {
    var output = $(this).val();
    $('#output').html(output);
});


var output = '';
$(':input').each(function() {
    output += $(this).val();
});
$('#output').html(output);

Or you could also use the .map() function:

var output = $(':input').map(function() {
    return $(this).val();
}).toArray().join('');
$('#output').html(output);


either try

var output = new Array();
$(':input').each(function() {
    output.push($(this).val());
});
alert(output);

DEMO

alternate:

var output = $(':input').map(function() {
    return $(this).val();
}).get();
alert(output);

DEMO

Reference

get

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜