我创建了这段代码:
<div class="option">
<label for="date">Date de debut de reservation</label>
<input type="date" id="datepickerFrom" ui-date="dateOptionsFrom" ng-model="datepickerFrom" class="form-control ng-empty hasDatepicker ng-touched ng-dirty ng-valid-parse ng-invalid ng-invalid-ui-date-validator" name="datepickersFrom" placeholder="dd/mm/yyyy" min="<?php echo $today; ?>" required>
<script>
var today = new Date().toISOString().split('T')[0];
document.getElementsByName("datepickersFrom")[0].setAttribute('min', today);
</script>
</div>
我想将所选日期与今天的日期进行比较。
如果是今天选择今天的情况,我想在下面运行以下代码:
$("#exptype").append('<option value="fast" selected>Ultra-express 1 heure</option>');
否则,如果未选择今天的日期,我想执行此代码:
$("#exptype").append('<option value="stand">Livraison standard 24-48h</option>');
我该怎么做?
答案 0 :(得分:0)
你正在使用棱角分明对吧?鉴于您在日期输入中设置了ng-model
属性,我认为这是假设的。如果是这种情况,您可以使用角度控制器来处理 - 请参见下面的示例。
另外,假设您正在使用angular,那么您可以使用angular date picker controls来获得更丰富/更强大的UI控件...
angular.module('myApp',[]).controller('dateController',function($scope) {
var today = new Date().toISOString().split('T')[0];
$scope.datepickerFrom = today;
$scope.todaySelected = true;
//this replaces that line you had:
//document.getElementsByName("datepickersFrom")[0].setAttribute('min', today);
$scope.minDate = today;
//when the date changes, store a boolean in our controller scope, to be used in the filter function below
$scope.dateChanged = function() {
$scope.todaySelected = ($scope.datepickerFrom == today);
};
//define our options - please adjust for your needs
$scope.expTypeOptions = [
{ value: "four hours", text: "4 heure" },
{ value: "fast", text: "Ultra-express 1 heure" },
{ value: "stand", text: "Livraison standard 24-48h" },
];
//this function handles showing the expType options in the select list,
//based on the selected date
$scope.filterOptionsByDate = function(expType) {
if ($scope.todaySelected) { //hide the 'stand' option
return expType.value != "stand";
}
else { //otherwise show everything except the 'fast' option
return expType.value != "fast";
}
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="option" ng-app="myApp" ng-controller="dateController">
<label for="date">Date de debut de reservation</label>
<input type="date" id="datepickerFrom" ui-date="dateOptionsFrom" ng-model="datepickerFrom" class="form-control ng-empty hasDatepicker ng-touched ng-dirty ng-valid-parse ng-invalid ng-invalid-ui-date-validator" min="{{ minDate }}" ng-change="dateChanged()" name="datepickersFrom" placeholder="dd/mm/yyyy" required >
<div>
<select ng-model="expType" ng-options="expType.text for expType in expTypeOptions | filter:filterOptionsByDate">
</select>
</div>