分别在鼠标滚轮向上/向下的datepicker中增加/减少日期

时间:2016-05-23 05:44:34

标签: javascript jquery html datepicker

我有datepicker。我希望在mouse wheel up被触发时将日期增加1天,并在mouse wheel down被触发时将日期减1天(直到最小可能日期)。

以下是我的代码段:

HTML

<input type="text" id="txtFromDate" class="calender"/>

JS

$(document).ready(function(){
$("#txtFromDate").datepicker({
    minDate: new Date()   
});
$("#txtFromDate").datepicker("setDate", new Date()); });


$('.calender').bind('mousewheel DOMMouseScroll', function (event) { 
    $id = $(this).attr('id');
    $minDate = $('#' + $id).datepicker("option", "minDate");
    $minDate = ($minDate.getMonth() + 1) + "/" + $minDate.getDate() + "/" + $minDate.getFullYear(); 
    $datePickerDate = $('#' + $id).datepicker("getDate");
    $datePickerDate = ($datePickerDate.getMonth() + 1) + "/" + $datePickerDate.getDate() + "/" + $datePickerDate.getFullYear(); 
  var days = parseInt("1", 10);

    if (event.originalEvent.wheelDelta > 0 || event.originalEvent.detail < 0) { 
        $datePickerDate = new Date($datePickerDate); 
        $datePickerDate = $datePickerDate.setDate($datePickerDate.getDate() + days);
    }
    else {
        if ($datePickerDate >= $minDate && $('#' + $id ).val() != "") {   
            $datePickerDate = new Date($datePickerDate);
            $datePickerDate = $datePickerDate.setDate($datePickerDate.getDate() - days);
        }
    }
    alert($datePickerDate);
    $('#' + $id).val($datePickerDate);
    $('#' + $id).datepicker("setDate", $datePickerDate);
});

$datePickerDate值将变为1464028200000

以下是Non-Working Demo

我做错了什么?

0 个答案:

没有答案