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
精彩评论