html - 显示从列表中的下拉列表中选择的多个值
问题描述
我有一个 angularjs 函数,其中包含一个值数组,如下所示
$scope.arrayVal = function()
{
var array = [{id:1,name:"one"},{id:2,name:"two"}];
}
我将此数组传递给我在 html 页面中的下拉列表,并在下拉列表中显示值,如下所示
<html>
<select ng-model="data.selected">
<option value="item.id,item.name" ng-repeat="item in array">{{item.id}} {{item.name}}</option>
</html>
我想要实现的是我想让用户从下拉列表中选择多个值,并且这些选择的值应该根据下拉列表下方的选择顺序显示。如何仅使用 angularjs 和 html 来实现这一点。(我没有使用除 angularjs 之外的任何库)
解决方案
试试这个插件https://next.plnkr.co/edit/6Nxaxz9hGNXospyT。你可能不得不修改它以获得你想要的结果
<div ng-controller="ctrl">
<select ng-model="selectedItem" ng-options="item.name for item in items" ng-change="selectItem()">
</select>
<p ng-repeat="item in selectedItems">{{item.id}} - {{item.name}}</p>
</div>
angular.module('app', [])
.controller('ctrl', function($scope) {
$scope.items = [{id:1,name:"one"},{id:2,name:"two"}];
$scope.selectedItems = [];
$scope.selectedItem;
$scope.selectItem = function () {
var index = $scope.selectedItems.findIndex(function (fItem) { return $scope.selectedItem.id === fItem.id; });
console.log(index)
if (index > -1) $scope.selectedItems.splice(index, 1);
else $scope.selectedItems.push($scope.selectedItem);
};
});
推荐阅读
- git - 来自 Windows 批处理文件的 Git 脚本
- powerbi - DAX:如何编写 IF 语句以返回所选多个(特定)值的计算?
- database - 我可以导出和导入我的数据库以“消除”任何内存页面问题吗
- eclipse - tomcat 9.0 服务器的状态未知或正在转换
- python - 逐块读取大文件并将其附加到python字典中
- java - 如何使用 Opencv 检测具有不同对比度和背景颜色的图像边缘?
- sql - 我想计算 DB2 游标中的行数
- mikrotik - 如何通过我的静态 IP 地址从外部网络访问 RouterOs(web 或 Winbox)
- java - Spring mvc 的问题:注释驱动
- python - 为 Linux 上的所有用户/所有代码启用 python 登录