Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

javascript - disabling specific dates from html datepicker jquery

hey guys i am trying to set a datepicker in my webpage and disable some dates from it so it can't be showing
this is the code:

<!doctype html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Untitled Document</title>

        <link href="jquery-ui/jquery-ui.css" rel="stylesheet">
        <script src="jquery-ui/external/jquery/jquery.js" ></script>
        <script src="jquery-ui/jquery-ui.js"></script>

        <script>

            /** Days to be disabled as an array */
            var disableddates = ["20-5-2015", "12-11-2014", "12-25-2014", "12-20-2014"];

            function DisableSpecificDates(date) {

                var m = date.getMonth();
                var d = date.getDate();
                var y = date.getFullYear();

                // First convert the date in to the mm-dd-yyyy format 
                // Take note that we will increment the month count by 1 
                var currentdate = (m + 1) + '-' + d + '-' + y;

                // We will now check if the date belongs to disableddates array 
                for (var i = 0; i < disableddates.length; i++) {

                    // Now check if the current date is in disabled dates array. 
                    if ($.inArray(currentdate, disableddates) != -1) {
                        return [false];
                    }
                }
            }

            $(function () {
                $("#date").datepicker({
                    beforeShowDay: DisableSpecificDates
                });
            });
        </script>
    </head>

    <body>
        <input id="date" type="text">
    </body>
</html>

but it is not working for some reason... the date picker don't even show can someone plz help

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this, run this code below. The first date I added a 0 to the day so that it will match the formatting in the comparison.

/** Days to be disabled as an array */
var disableddates = ["20-05-2015", "12-11-2014", "12-25-2014", "12-20-2014"];


function DisableSpecificDates(date) {
    var string = jQuery.datepicker.formatDate('dd-mm-yy', date);
    return [disableddates.indexOf(string) == -1];
  }
  /*
var m = date.getMonth();
var d = date.getDate();
var y = date.getFullYear();

// First convert the date in to the mm-dd-yyyy format 
// Take note that we will increment the month count by 1 
var currentdate = (m + 1) + '-' + d + '-' + y ;

// We will now check if the date belongs to disableddates array 
for (var i = 0; i < disableddates.length; i++) {

// Now check if the current date is in disabled dates array. 
if ($.inArray(currentdate, disableddates) != -1 ) {
return [false];

}
}

}*/


$(function() {
  $("#date").datepicker({
    beforeShowDay: DisableSpecificDates
  });
});
<!doctype html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Untitled Document</title>

  <link href="http://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css" rel="stylesheet">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="http://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script>
</head>

<body>

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


</body>

</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...