如何在 jQuery datepicker 中每周随机启用一天

目前我只在日期选择器中显示接下来的 60 天。我试图每周只随机启用一天。我不知道怎么做。


  $( "#visit" ).datepicker({

   beforeShowDay: $.datepicker.noWeekends,

   minDate: 0,

   maxDate:60

   });

http://jsfiddle.net/4zb2frxt/2/


红颜莎娜
浏览 156回答 1
1回答

临摹微笑

您可以编写自定义函数来禁用除一周中的某一天之外的所有日期,以及下面的代码$(function(){&nbsp; &nbsp; $("#visit").datepicker({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;beforeShowDay: available,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;minDate: 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;maxDate:60&nbsp; &nbsp; });&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; //get random value to select date&nbsp; &nbsp; &nbsp; var random = Math.floor(Math.random() * 5) + 4;&nbsp; &nbsp; &nbsp; //console.log(random);&nbsp; &nbsp; function available(date) {&nbsp; &nbsp; &nbsp; var day = date.getDay();&nbsp; &nbsp; &nbsp; //disable weekends&nbsp; &nbsp; &nbsp; if(day==6 || day==0) {&nbsp; &nbsp; &nbsp; &nbsp; return [false,"","unAvailable"];&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; var dt = date.getDate();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; if (dt % random ==0) {&nbsp; &nbsp; &nbsp; &nbsp; return [true, "","Available"];&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return [false,"","unAvailable"];&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }});<link rel="stylesheet" type="text/css" href="http://davidwalsh.name/dw-content/jquery-ui-css/custom-theme/jquery-ui-1.7.2.custom.css"><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script><script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script><input type='text' id='visit' />
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript