Javascript 通过ng的多个下拉选项不起作用
我们在表单中通过ng选项进行多个下拉,但如果我们更改一个下拉,则所选值也会关联到另一个下拉Javascript 通过ng的多个下拉选项不起作用,javascript,angularjs,Javascript,Angularjs,我们在表单中通过ng选项进行多个下拉,但如果我们更改一个下拉,则所选值也会关联到另一个下拉 <div ng-app="SelectApp"> <div ng-controller="selectController"> <select name="category-group" id="categoryGroup" class="form-control" ng-model="itemSelected" ng-change="onCategoryC
<div ng-app="SelectApp">
<div ng-controller="selectController">
<select name="category-group" id="categoryGroup" class="form-control" ng-model="itemSelected" ng-change="onCategoryChange(itemSelected)"
ng-options="category.name group by category.group for category in categories">
</select>
<select name="category-group" id="categoryGroup2" class="form-control" ng-model="itemSelected" ng-change="onCategoryChange(itemSelected)"
ng-options="category.name group by category.group for category in categories">
</select>
</div>
我改变了你的代码如下。希望这能奏效 app.js
$scope.itemSelected = $scope.categories[0];
$scope.itemSelected1 = $scope.categories[0];
$scope.onCategoryChange = function (item,index) {
$window.alert("Selected Value: " + item.id + "\nSelected Text: " + item.name);
};
}]);
看法
对于两个下拉列表,您使用的是相同的ng模型,因此angular会将该值更新为其模型。ng model=itemSelected为每个选择框使用单独的模型感谢您的快速响应,但是这些下拉列表来自某个后端系统的ng repeat。我想将不同的值关联到Alerts中的不同下拉列表,两者都将类别名称显示为值,是否有任何方法可以像demohi srin的选择框id中那样在ng型号的末尾附加数字,谢谢您的帮助,但我的问题仍然没有解决我正在更新我的fiddler,当我从第一个下拉列表中选择我在xyz中绑定的值时,它正确显示,但当我从第二个下拉列表中选择它更新第一个和第二个值时,它们都使用相同的值,我希望它的不同值选择单独更新检查您的小提琴
$scope.itemSelected = $scope.categories[0];
$scope.itemSelected1 = $scope.categories[0];
$scope.onCategoryChange = function (item,index) {
$window.alert("Selected Value: " + item.id + "\nSelected Text: " + item.name);
};
}]);
<div ng-app="SelectApp">
<div ng-controller="selectController">
<select name="category-group" id="categoryGroup" class="form-control" ng-model="itemSelected" ng-change="onCategoryChange(itemSelected)"
ng-options="category.name group by category.group for category in categories">
</select>
<select name="category-group" id="categoryGroup" class="form-control" ng-model="itemSelected1" ng-change="onCategoryChange(itemSelected1)"
ng-options="category.name group by category.group for category in categories">
</select>
</div>