2017-09-05 206 views
0

我正在使用jQuery UI datepicker,当用户在今天的日期或今天的日期之前的任何以前的日期点击时,我试图显示一个警报,但我不知道如何,我设法获取每个日期平变化,但我不能做休息,这里是选择今天的日期我的jQuery代码:jQuery UI datepicker获取今天的日期或之前的日期

$(document).on("change", "#datepicker", function() { 
    alert($(this).val()) 
}) 
+0

你将不得不使用JavaScript的日期()函数,然后比较你得的是该数值或日期小于今天的或以前日期 – clearshot66

+0

@ j08691感谢您的嘲讽:) – clearshot66

+0

@ clearshot66好吧,在您将评论从“您需要JavaScript”更改为 – j08691

回答

0
This code find currrent date and if block compare the selected date is current date or not and then prompt alert! 

    <!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <title>jQuery UI Datepicker - Default functionality</title> 
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> 
    <link rel="stylesheet" href="/resources/demos/style.css"> 
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script> 
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
    <script> 
    $(function() { 
    $("#datepicker").datepicker(); 
var today = new Date(); 
var date = today.getDate(); 
var mnth = today.getMonth()+1; 

var yyyy = today.getFullYear(); 
if(date<10){ 
    date='0'+date; 
} 
if(mnth<10){ 
    mnth='0'+mnth; 
} 
var today = mnth+'/'+date+'/'+yyyy; 
alert(today); // Display current date 


    $(document).on("change", "#datepicker", function() { 
     if($(this).val()<=today) 
     { 
     alert($(this).val()) 
     } 
    }); 

    }); 


    </script> 
</head> 
<body> 

<p>Date: <input type="text" id="datepicker"></p> 


</body> 
</html> 
+0

之前,我想检索今天的日期onclick而不是加载,并且此代码重新加载在今天的日期之后,我只想要以前的日期 – jessica

0

使用getDate方法你可以简单地得到选择的日期,并将其与比较当前时间。您可以使用JavaScript日期valueOf()获取:

UTC 1970年1月1日00:00:00与给定日期之间的毫秒数。

这里工作示例:

$('#datepicker').datepicker(); 
 

 
$(document).on("change", "#datepicker", function() { 
 
    // Get datepicker value 
 
    var selectedDate = $(this).datepicker("getDate"); 
 
    // Get current time 
 
    var today = new Date().valueOf(); 
 
    // Compare current time and selected date 
 
    if(selectedDate && selectedDate.valueOf() < today){ 
 
    alert('You selected a past date: ' + $(this).val()) 
 
    } 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.js"></script> 
 
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.css" rel="stylesheet"/> 
 

 
<input type="text" id="datepicker">

相关问题