gpt4 book ai didi

javascript - 提交表单时如何从多选中获取数组[AngularJS]

转载 作者:行者123 更新时间:2023-12-03 08:31:23 27 4
gpt4 key购买 nike

提交多选框时,我需要获取带有 ids 的数组。那么,该数组应该如下所示:

[13,14,15]

13,14,15 是对象内的 id...

这是我的选择框的样子:

<select ng-model="groups.group" multiple="multiple" class="form-control ng-pristine ng-valid ng-touched" ng-options="group as group.name for group in groups.groups">
<option value="object:13" label="Group4">Group4</option>
<option value="object:14" label="Group6">Group6</option>
<option value="object:15" label="Group7">Group7</option>
</select>

这是我在模板内的 Angular 代码:

<select ng-model="groups.group" multiple="multiple" class="form-control" ng-options="group as group.name for group in groups.groups">

最佳答案

var app = angular.module('myApp', []);

app.controller('appCtrl', function($scope) {
$scope.groups = [1, 2, 3, 4, 5];
$scope.selectedGroups = [];
$scope.addedGroups = [];

$scope.add = function() {
$scope.addedGroups = $scope.selectedGroups;
};
});
select,
div {
width: 300px;
}
div {
background-color: yellow;
display: block;
height: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="appCtrl">
<select multiple="multiple" ng-model="selectedGroups">
<option ng-repeat="group in groups" value="{{group}}">{{group}}</option>
</select>
<button ng-click="add()">add to box</button>
<div>{{addedGroups}}</div>
{{selectedGroups}}
</div>

关于javascript - 提交表单时如何从多选中获取数组[AngularJS],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33322158/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com