Javascript 如何获取下拉列表的值
这是我的HTMLJavascript 如何获取下拉列表的值,javascript,php,jquery,html,angularjs,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
<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>
选择高度
123厘米
125厘米
124厘米
拯救
在控制器中,我是这样做的
userHeight=$scope.userHeight代码>
但我没有得到任何价值
如何获取该值?您错误地使用了ng model
作为选项它仅用于输入框、文本区域、选择等输入字段
就像
<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>
选择高度
123厘米
125厘米
124厘米
现在,只有您得到的值是您错误地使用ng model作为选项的值它仅用于输入框、文本区域、选择等输入字段
就像
<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>
选择高度
123厘米
125厘米
124厘米
现在只有您获得值将ng模型放入select。
此外,如果要选择1项默认值,则
$scope.userHeight=“/value of option/” 将ng模型放入select。
此外,如果要选择1项默认值,则
$scope.userHeight=“/value of option/” 给你:
var-app=angular.module('app',[]);
app.controller('controller',['$scope',controllerFunction]);
函数控制器函数($scope){
$scope.companies=[{
“id”:1,
“名称”:“c1”
}, {
“id”:2,
“名称”:“c2”
}];
$scope.companyChange=函数(){
警报(angular.toJson($scope.company));
};
}
全部的
给您:
var-app=angular.module('app',[]);
app.controller('controller',['$scope',controllerFunction]);
函数控制器函数($scope){
$scope.companies=[{
“id”:1,
“名称”:“c1”
}, {
“id”:2,
“名称”:“c2”
}];
$scope.companyChange=函数(){
警报(angular.toJson($scope.company));
};
}
全部的
在另一个问题中查看我的示例代码,该问题涉及不同的问题—选择元素并使用ng选项生成列表。select元素上的ng模型是如何更新模型的。请参阅另一个问题中的示例代码,该问题与select元素上的ng_模型有关,并使用ng选项生成列表。select元素上的ng模型是更新模型的方式。