如何获得下拉值

时间:2015-07-08 10:21:19

标签: javascript php jquery html angularjs

这是我的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;

但我没有任何价值。

我如何获得价值?

3 个答案:

答案 0 :(得分:2)

您错误地使用ng-model作为选项。 仅用于输入框,textarea,select

等输入字段

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>

现在只有你获得值

答案 1 :(得分:1)

将ng-model放入select中。 如果你想选择1项默认值 $ scope.userHeight =“/ option of option /”;

答案 2 :(得分:1)

你在这里:

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

希望这有帮助。