2017-08-08 31 views
1

這是一個花店,當一個客戶填寫表格時,他們必須選擇一個日期(jquery datepicker)和交貨時間(選擇字段)。星期六有不同的交貨時間從太陽 - 星期五。我創建了兩個不同的發貨時間字段,並希望隱藏星期六,除非客戶在日期選擇器上選擇星期六。如何在jquery datepicker中選擇星期六時隱藏元素並顯示另一個元素?

這是我迄今爲止..

$('.mydatepicker').datepicker(function() { 
if ($(day == 6)) { 
    $('#delivery_time_normal').hide(); 
    } else { 
    $('#delivery_time_saturday').show(); 
} 
}); 
+0

您需要使用在該日期選擇器記錄的事件API。你使用哪種日期選擇器......有很多呢? – charlietfl

+0

jQuery UI日期選擇器:https://jqueryui.com/datepicker/ – epochcoding

回答

0

可以使用onSelect事件捕獲到datepicker用戶選擇。 此事件還接收選定的日期爲string,您可以解析爲Date,並獲得相應的工作日,像這樣:

$(function() { 
 
    $('#mydatepicker').datepicker({ 
 
     onSelect: function(date) { 
 
      var day = new Date(date).getDay(); 
 
      console.log("Selected weekday " + day); 
 

 
      if (day == 6) { 
 
       $('#delivery_time_normal').hide(); 
 
      } else { 
 
       $('#delivery_time_saturday').show(); 
 
      } 
 
     } 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script 
 
    src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" 
 
    integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" 
 
    crossorigin="anonymous"></script> 
 
    
 
    <input type="text" id="mydatepicker">

0

試試這個,希望這會幫助你,

$('.mydatepicker').datepicker(function() { 
    onSelect: function(dateText, inst) { 
    var date = $(this).datepicker('getDate'); 
    var day = date.getDay(); 

if (day == 6) { 
    $('#delivery_time_normal').hide(); 
    } else { 
    $('#delivery_time_saturday').show(); 
} 

} 
}); 
相關問題