admin管理员组文章数量:1289845
I have numerous text boxes on my page, and I want to add the values from them to another element when a button is clicked. However, my current code outputs the values like this:
Value oneValue twoValue three
I would much prefer for them to e out like this instead:
Value one, Value two, Value three
my current JS is like this:
var textInput = $(".random-input").text();
$(".output-box").append(textInput);
I have numerous text boxes on my page, and I want to add the values from them to another element when a button is clicked. However, my current code outputs the values like this:
Value oneValue twoValue three
I would much prefer for them to e out like this instead:
Value one, Value two, Value three
my current JS is like this:
var textInput = $(".random-input").text();
$(".output-box").append(textInput);
Share
Improve this question
edited Apr 7, 2016 at 16:24
Rory McCrossan
338k41 gold badges320 silver badges351 bronze badges
asked Apr 7, 2016 at 16:20
Sam WillisSam Willis
4,2117 gold badges44 silver badges63 bronze badges
1
- 3 @u_mulder I did what's the problem – digglemister Commented Apr 7, 2016 at 16:29
3 Answers
Reset to default 9The issue is because the elements are all being interpreted together. To fix that you can map()
the text values to an array and join()
it:
var textInput = $(".random-input").map(function() {
return $(this).val();
}).get().join(', ');
$(".output-box").text(textInput);
Working example
The above is assuming .output-box
is a standard element. If it's another textbox, you'd need to use val(textInput)
.
Update: Oct 2020
The above example can now also be made more succinct by using an ES6 arrow function, but note that this is unsupported in IE.
var textInput = $(".random-input").map((i, el) => el.value).get().join(', ');
$(".output-box").text(textInput);
You can iterate through each input and then using join
var textInput = [];
$(".random-input").each(function() {
textInput.push(this.value)
});
$(".output-box").append(textInput.join(', '));
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='text' class='random-input' value='val1' />
<input type='text' class='random-input' value='val1' />
<input type='text' class='random-input' value='val1' />
<input type='text' class='random-input' value='val1' />
<input type='text' class='random-input' value='val1' />
<input type='text' class='random-input' value='val1' />
<input type='text' class='random-input' value='val1' />
<div class='output-box'></div>
@Rory McCrossan code is moved to the jQuery function joinVal:
jQuery.fn.extend({
joinVal: function (separator)
{
if (typeof separator === 'undefined')
separator = ",";
var $this = $(this);
if ($this && $this.length > 1)
return $this.map(function () { return $(this).val(); }).get().join(separator);
else
return $this.val();
}
});
Usage:
var textInput = $(".random-input").joinVal();
https://jsfiddle/NickU/4j892h0u/3/
本文标签: javascriptjQuery find elements then make into comma separated listStack Overflow
版权声明:本文标题:javascript - jQuery find elements then make into comma separated list - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741438725a2378790.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论