2016-11-14 10 views
-4

誰かがコードを変更するのを手伝ってもらえますか?メンバーが3月〜10月(毎年)の日曜日を選択できるようにしたいと思います。 しかし、その月の第2または第4日曜日のみ。JQuery Datepicker

ありがとうございました!

<!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 type="text/javascript"> 
 
    $(function() { 
 
     $("#datepic").datepicker(
 
     { beforeShowDay: function(day) { 
 
      var day = day.getDay(); 
 
      if (day == 2 || day == 5| day == 4| day == 6| day == 1| day == 3) 
 
{ 
 
       return [false, "somecssclass"] 
 
      } else { 
 
       return [true, "someothercssclass"] 
 
      } 
 
     } 
 
     }); 
 
    }); 
 
</script> 
 
</head> 
 
<body> 
 
<p>Uw voorkeursdatum: <input id="datepic"/> 
 
</body> 
 
</body> 
 
</html>

+1

あなたは試してみて、起動開発をし、何か問題がある場合は質問をしたり、エラーが表示されるはずです。 –

+0

この回答はあなたのやり方に役立つはずhttp://stackoverflow.com/a/9295262/2822450 – developius

+0

日曜日のみ選択できるように変更しましたが、今は何ですか?私は毎月2回目と4回目の日曜日のみ有効にしたい。 – tiresz

答えて

0

beforeShowDayオプションが選ばれる第二と第四日曜日を可能にするために適切な場所です。

これを行うために、2つの "フラグ"(ループ内の特定の状態を判断するために使用される変数)を使用しました。

ループ内で日曜日が切り替わるたびに切り替わる1つのフラグ。
今月の日曜日のみを考慮してください。

DatePickerは、beforeShowDayオプションで提供されている関数の各テーブルセルをループしています。
"レンダリング"されていなくても、前月または次月の日はこのループを経由してです。

また、月が1回描かれたら、これらのフラグをリセットするために100msのタイムアウトを使用しました。
月から月にナビゲートするときに必要です。

this CodePenで機能する関数は次のとおりです。
私は便利なカップルを残しましたconsole.log
;)

$(function() { 
    var even=false; 
    var inCurrentMonth=false; 

    $("#DatePicker").datepicker({ 

     beforeShowDay: function(fulldate) { 
      //console.log(fulldate); 
      var day = fulldate.getDay(); 
      var date = fulldate.getDate(); 

      // In current month when the date 1 is found. 
      if(date==1){ 
       inCurrentMonth=!inCurrentMonth; 
       // To reset values right after month draw. 
       setTimeout(function(){ 
        even=false; 
        inCurrentMonth=false; 
       },100); 
      } 

      // This condition prevents conting syndays frome the previous month. 
      if(inCurrentMonth){ 
       // If NOT a sunday. 
       if (day != 0){ 
        return [false,"Otherdays"] 
       } else { 
        // If this sunday is even (2nd or 4th) 
        if(even){ 
         even=!even; 
         return [true,"Selectable-Sunday","You can select me!"] 
        }else{ 
         even=!even; 
         return [false,"Unselectable-Sunday"] 
        } 
       } 
      } 
     } 
    }).focus(); 
});