如何将 select 选项标签字段的值或 selected 文本传递给函数参数
How to pass select option label field's value or selected text to a functions argument
我的要求是获取所选选项并更新 div。我在下面的代码中模拟了我的要求。
我应该让用户只选择 startDate 和 endDate 之间的月末(我在下面的代码中完成了这部分)。但我无法用所选日期更新。我只能访问 Not the label or text 中的 "value" 属性。
http://jsfiddle.net/priyaa2002/xpcbounr/3/
<div ng-app="myApp">
<div ng-controller="DateCtrl">
<div class="edit__table__field"><p>{{myEnrollment.coverageEndDate}}</p></div>
<select ng-model="myEnrollment.coverageEndDate" ng-options="value as coverageEndDate for (value, coverageEndDate) in
terminationDates"
ng-change="newTerminationDate(myEnrollment.coverageEndDate)" >
<option value="">Select your new Termination Date</option>
</select>
</div>
</div>
JS
angular.module('myApp', [])
.controller('DateCtrl', function($scope){
$scope.myEnrollment = {
coverageStartDate: '01/01/2016',
coverageEndDate: '06/01/2016'
};
var startDate = new Date ($scope.myEnrollment.coverageStartDate);
var endDate = new Date ($scope.myEnrollment.coverageEndDate);
var startMonth = startDate.getMonth();
var endMonth = endDate.getMonth();
var year = endDate.getFullYear();
function convertDate(date) {
var parts = date.toString().split(" ");
var months = {
Jan: "01",
Feb: "02",
Mar: "03",
Apr: "04",
May: "05",
Jun: "06",
Jul: "07",
Aug: "08",
Sep: "09",
Oct: "10",
Nov: "11",
Dec: "12"
};
return months[parts[1]]+"/"+parts[2]+"/"+parts[3];
};
var pickDates = endMonth - startMonth;
$scope.terminationDates = [];
for (var i=0; i < pickDates; i++){
var lastDay = new Date(year, i + 1, 0);
var formatedDate = convertDate(lastDay);
$scope.terminationDates.push(formatedDate);
}
$scope.newTerminationDate = function(coverageDate) {
$scope.myEnrollment.coverageDate = "hello";
}
});
你可以简单地做:
<select ng-model="myEnrollment.coverageEndDate" ng-options="value as value for value in terminationDates" ng-change="newTerminationDate(myEnrollment.coverageEndDate)" >
<option value="">Select your new Termination Date</option>
</select>
希望对您有所帮助!
我的要求是获取所选选项并更新 div。我在下面的代码中模拟了我的要求。
我应该让用户只选择 startDate 和 endDate 之间的月末(我在下面的代码中完成了这部分)。但我无法用所选日期更新。我只能访问 Not the label or text 中的 "value" 属性。
http://jsfiddle.net/priyaa2002/xpcbounr/3/
<div ng-app="myApp">
<div ng-controller="DateCtrl">
<div class="edit__table__field"><p>{{myEnrollment.coverageEndDate}}</p></div>
<select ng-model="myEnrollment.coverageEndDate" ng-options="value as coverageEndDate for (value, coverageEndDate) in
terminationDates"
ng-change="newTerminationDate(myEnrollment.coverageEndDate)" >
<option value="">Select your new Termination Date</option>
</select>
</div>
</div>
JS
angular.module('myApp', [])
.controller('DateCtrl', function($scope){
$scope.myEnrollment = {
coverageStartDate: '01/01/2016',
coverageEndDate: '06/01/2016'
};
var startDate = new Date ($scope.myEnrollment.coverageStartDate);
var endDate = new Date ($scope.myEnrollment.coverageEndDate);
var startMonth = startDate.getMonth();
var endMonth = endDate.getMonth();
var year = endDate.getFullYear();
function convertDate(date) {
var parts = date.toString().split(" ");
var months = {
Jan: "01",
Feb: "02",
Mar: "03",
Apr: "04",
May: "05",
Jun: "06",
Jul: "07",
Aug: "08",
Sep: "09",
Oct: "10",
Nov: "11",
Dec: "12"
};
return months[parts[1]]+"/"+parts[2]+"/"+parts[3];
};
var pickDates = endMonth - startMonth;
$scope.terminationDates = [];
for (var i=0; i < pickDates; i++){
var lastDay = new Date(year, i + 1, 0);
var formatedDate = convertDate(lastDay);
$scope.terminationDates.push(formatedDate);
}
$scope.newTerminationDate = function(coverageDate) {
$scope.myEnrollment.coverageDate = "hello";
}
});
你可以简单地做:
<select ng-model="myEnrollment.coverageEndDate" ng-options="value as value for value in terminationDates" ng-change="newTerminationDate(myEnrollment.coverageEndDate)" >
<option value="">Select your new Termination Date</option>
</select>
希望对您有所帮助!