【问题标题】:How to get values of dropdown如何获取下拉列表的值
【发布时间】:2015-07-08 18:28:53
【问题描述】:
这是我的 HTML
<select class="height_selected">
<option>Select Height</option>
<option ng-model="userHeight" ng-init="1" value="1">123 cm</option>
<option ng-model="userHeight" ng-init="2" value="2">125 cm</option>
<option ng-model="userHeight" ng-init="3" value="3">124 cm</option>
</select>
<a type="button" ng-click="updateHeight()">Save</a></div>
在控制器中我做
userHeight = $scope.userHeight;
但我没有得到任何价值。
如何获得价值?
【问题讨论】:
标签:
javascript
php
jquery
html
angularjs
【解决方案1】:
您错误地将ng-model 用作选项。 仅用于输入框、文本区域、选择等输入字段
For More Reference
它就像
<select class="height_selected" ng-model="userHeight">
<option>Select Height</option>
<option value="1">123 cm</option>
<option value="2">125 cm</option>
<option value="3">124 cm</option>
</select>
现在只有你获得价值
【解决方案2】:
将 ng-model 放入选择中。
另外,如果您想选择 1 项默认值
$scope.userHeight = "/选项值/";
【解决方案3】:
你在这里:
var app = angular.module('app', []);
app.controller('controller', ['$scope', controllerFunction]);
function controllerFunction($scope) {
$scope.companies = [{
'id': 1,
'name': "c1"
}, {
'id': 2,
'name': "c2"
}];
$scope.companyChange = function() {
alert(angular.toJson($scope.company));
};
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="controller">
<select ng-model="company" ng-options="c.name for c in companies track by c.id" ng-change="companyChange()">
<option value="">All</option>
</select>
</div>
参考更多:https://docs.angularjs.org/api/ng/directive/ngOptions
希望这会有所帮助。