如何将数组值传递给输入字段的 onChange 函数?
How to pass array values to function onChange from input field?
我正在调用一个名为 "VizDateRange" 的函数,它在选择日期时接收两个参数,这些选定的值应作为两个值传递,但选定的日期采用奇怪的格式,显示为 02/01/ 2017-06/19/2017 作为一个值。那么,为了将它作为两个值传递,我应该如何进行?
//HTML
<div class="input-group date" data-provide="datepicker" name="" title="Select date range">
<input type="text" id="daterange" class="form-control" onChange=VizFilter(this.val().split(" - "))> //this is not working obviously.
</div>
//调用bootstrap的日期范围插件daterangepicker
$(document).ready(function () {
var dp = $('#daterange').daterangepicker({
"startDate": "02/06/2017", setting the limit for min
"endDate": "06/19/2017"setting the limit for max
}
)
});
//输出为
02/01/2017-06/19/2017
// 但我需要的是“02/01/2017”、“06/19/2017”,以便将其传递给 VizDateRange 函数。
//所以我可以使用 split (" - ") 将其转换为数组,但是如何从输入字段传递这个 onChange 或 onClick?
//函数应该被调用 onChange 或 onCLick 并且它要求两个值,它接收两个参数
function vizDateRange(minDate,maxDate)
{
sheet = viz0.getWorkbook().getActiveSheet();
worksheetsArray = sheet.getWorksheets();
for (var i = 0; i < worksheetsArray.length; i++) {
worksheetsArray[i].applyRangeFilterAsync("WEEK(DCM Date)", {
min: new Date(minDate),
max: new Date(maxDate)
});
}
};
//这里是jsfiddle
如果您查看 library's examples,第一个参数是一个选项对象,但作为第二个可选参数,您可以传递一个回调函数。以下是我将如何使用该回调来解决此问题:
$(document).ready(function() {
var dp = $('#daterange').daterangepicker({
"startDate": "02/06/2017",
"endDate": "06/19/2017"
},
function(start, end, label) {
start = moment(start).format('MM/DD/YYYY');
end = moment(end).format('MM/DD/YYYY');
vizDateRange(start, end)
}
)
});
function vizDateRange(minDate, maxDate) {
// do stuff here
console.log(minDate, maxDate);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Include Required Prerequisites -->
<script type="text/javascript" src="//cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<!-- Include Date Range Picker -->
<script type="text/javascript" src="//cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.css" />
<div class="input-group date" data-provide="datepicker" name="" title="Select date range">
<input type="text" id="daterange" class="form-control" > //this is not working obviously.
</div>
我正在调用一个名为 "VizDateRange" 的函数,它在选择日期时接收两个参数,这些选定的值应作为两个值传递,但选定的日期采用奇怪的格式,显示为 02/01/ 2017-06/19/2017 作为一个值。那么,为了将它作为两个值传递,我应该如何进行?
//HTML
<div class="input-group date" data-provide="datepicker" name="" title="Select date range">
<input type="text" id="daterange" class="form-control" onChange=VizFilter(this.val().split(" - "))> //this is not working obviously.
</div>
//调用bootstrap的日期范围插件daterangepicker
$(document).ready(function () {
var dp = $('#daterange').daterangepicker({
"startDate": "02/06/2017", setting the limit for min
"endDate": "06/19/2017"setting the limit for max
}
)
});
//输出为
02/01/2017-06/19/2017
// 但我需要的是“02/01/2017”、“06/19/2017”,以便将其传递给 VizDateRange 函数。
//所以我可以使用 split (" - ") 将其转换为数组,但是如何从输入字段传递这个 onChange 或 onClick?
//函数应该被调用 onChange 或 onCLick 并且它要求两个值,它接收两个参数
function vizDateRange(minDate,maxDate)
{
sheet = viz0.getWorkbook().getActiveSheet();
worksheetsArray = sheet.getWorksheets();
for (var i = 0; i < worksheetsArray.length; i++) {
worksheetsArray[i].applyRangeFilterAsync("WEEK(DCM Date)", {
min: new Date(minDate),
max: new Date(maxDate)
});
}
};
//这里是jsfiddle
如果您查看 library's examples,第一个参数是一个选项对象,但作为第二个可选参数,您可以传递一个回调函数。以下是我将如何使用该回调来解决此问题:
$(document).ready(function() {
var dp = $('#daterange').daterangepicker({
"startDate": "02/06/2017",
"endDate": "06/19/2017"
},
function(start, end, label) {
start = moment(start).format('MM/DD/YYYY');
end = moment(end).format('MM/DD/YYYY');
vizDateRange(start, end)
}
)
});
function vizDateRange(minDate, maxDate) {
// do stuff here
console.log(minDate, maxDate);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Include Required Prerequisites -->
<script type="text/javascript" src="//cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<!-- Include Date Range Picker -->
<script type="text/javascript" src="//cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.css" />
<div class="input-group date" data-provide="datepicker" name="" title="Select date range">
<input type="text" id="daterange" class="form-control" > //this is not working obviously.
</div>