在JavaScript

时间:2019-09-25 19:47:08

标签: javascript html

我使用以下表格选择日期:

<div class="form-group">
  <label for="id_vehicle">Request for: <?php echo $vehicle->brand?> <?php echo $vehicle->model?></label>
  <input type="hidden" id="id_vehicle" name="id_vehicle" value = "<?php echo $vehicle->id_vehicle?>">
  <br>
  <label for="from">From</label>
  <input type="date" id="from" name="from">
  <label for="to">to</label>
  <input type="date" id="to" name="to">
  </div>
  <div class="form-group" style="padding-bottom: 1em;">
  <input type="submit" class="btn btn-primary col text-center"  value="Request">
  </div>
  </form>
</div>

我需要将from日期的最小值作为今天的日期,并将to日期的from日期> =。我正在尝试使用datepicker这样做,但是我是使用javascript的新手,所以我想知道是否有人可以帮助我。谢谢!

1 个答案:

答案 0 :(得分:1)

这是可以完成的方法。希望对您有所帮助(:

//JavaScript:
function setToDate(val){
    document.getElementById("to").setAttribute("min", val);
}

var today = new Date(),
    day = today.getDate(),
    month = today.getMonth()+1, //January is 0
    year = today.getFullYear();
    
    if(day<10){
      day='0'+day
    } 
    if(month<10){
      month='0'+month
    }
    today = year+'-'+month+'-'+day;

    document.getElementById("from").setAttribute("min", today);
    document.getElementById("to").setAttribute("min", today);
<div class="form-group">
  <form>
  <label for="id_vehicle">Request for: <?php echo $vehicle->brand?> <?php echo $vehicle->model?></label>
  <input type="hidden" id="id_vehicle" name="id_vehicle" value = "<?php echo $vehicle->id_vehicle?>">
  <br>
  <label for="from">From</label>
  <input type="date" id="from" name="from" onchange="setToDate(this.value)">
  <label for="to">to</label>
  <input type="date" id="to" name="to">
  <div class="form-group" style="padding-bottom: 1em;">
  <input type="submit" class="btn btn-primary col text-center"  value="Request">
  </div>
  </form>
</div>