如何将数组值从输入字段传递给onChange函数?

时间:2017-07-17 02:52:19

标签: javascript jquery tableau daterangepicker

我正在调用名为" 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>

//调用daterangepicker,这是bootstrap的日期范围插件

   $(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 /二千零十七分之一十九

//但我需要的是&#34; 02/01 / 2017&#34;,&#34; 06/19/2017&#34;为了将它传递给VizDateRange函数。

//所以我可以使用split(&#34; - &#34;)将其转换为数组但是如何从输入字段传递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

https://jsfiddle.net/u4t1rebs/

1 个答案:

答案 0 :(得分:0)

如果查看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>