使用JQUERY Val();作为多元素获取器 [英] Using JQUERY Val(); as Multiple Element Getter
本文介绍了使用JQUERY Val();作为多元素获取器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用JQUERY Val();要将数据从amCharts期间选择器获取到页面上的div上,它可以工作,但只带一个值,.amChartsInputField有两个包含文本的输入区域.
I am using JQUERY Val(); to get the data from my amCharts period selector to a div on my page it works but only brings in one value, the .amChartsInputField has two input areas that contain text.
在JQUERY文档中,我注意到Val();.简单地使用它只会带来一个元素.有没有一种方法可以在一周内引入多个对象.
I have noticed on the JQUERY documtation that Val(); used plainly does only brings in one element. Is there a way a way to tweek this to bring in multiple objects.
下面的我的脚本:
JQUERY
chart1.addListener("rendered",function(e) {
e.chart.periodSelector.addListener("changed",function() {
var date = $(".amChartsPeriodSelector .amChartsInputField").val();
$("#date-div p").text( date );
HTML
<div id="date-div"><p>information pulled</p></div>
非常感谢您的帮助.
推荐答案
您可以使用jQuery的 map()
函数:
You could collect all of the values into an array using jQuery's map()
function:
// for each match, "map" the value into an array
var dates = $(".amChartsPeriodSelector .amChartsInputField").map(function() {
return $(this).val();
}).get();
// join the values together and assign to the date-div
$("#date-div p").text(dates.join(', '));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- made up HTML that fits your selectors -->
<div class="amChartsPeriodSelector">
<input type="text" class="amChartsInputField" value="2014-10-10" />
<input type="text" class="amChartsInputField" value="2015-11-10" />
</div>
<div id="date-div"><p></p></div>
这篇关于使用JQUERY Val();作为多元素获取器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文