开发者

Getting the text of all inputs in a form using jquery

开发者 https://www.devze.com 2023-03-04 18:08 出处:网络
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

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

精彩评论

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