2017-07-30 63 views
0
<form method="POST" enctype="multipart/form-data"> 
    CheckIn: <input type="date" name="datein" min="<?php echo date('Y-m-d'); 
    if(!empty($_POST['datein'])) 
      { 
       $get = $_POST['datein']; 
      } ?>" max="2030-12-31"> 
    CheckOut: <input type="date" name="dateout" min="<?php echo $get; ?>" max="2030-12-31"> 
</form> 

我知道這是一個錯誤的代碼。但是我想要的是,如果我單擊CheckIn日期,CheckOut的min也將更改,即使我再次點擊CheckIn。我認爲在JavaScript中有一個解決方案,但我不知道如何在那裏構建代碼。如果我點擊其他日期更改最短日期

回答

0

這是您的工作演示,驗證max。日期應不大於max

$('input[name=datein]').on('change', function() { 
 
    if ($(this).val() < $('input[name=dateout]').attr('max')) { 
 
    console.log('Before: ' + $('input[name=dateout]').attr('min')); 
 
    $('input[name=dateout]').attr('min', $(this).val()); 
 
    console.log('After: ' + $('input[name=dateout]').attr('min')); 
 
    } else { 
 
    console.log('Value greater than ' + $('input[name=dateout]').attr('max') + ' not allowed'); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
CheckIn: <input type="date" name="datein" min="" max="2030-12-31"> CheckOut: <input type="date" name="dateout" min="" max="2020-12-31">

0

在jQuery中,是這樣的:

$('input[name=datein]').change(function() { 
    // TODO Validate the value 
    $('input[name=dateout]').attr('min',$(this).val()); 
}); 

這設置min值等於datein控制的值。

.prop在這裏可能比.attr更好,但我認爲或者可以工作。