AngularJs实现Select二级联动

本文介绍如何使用AngularJS实现下拉框的级联选择功能,包括选择班级后自动更新显示该班级的学生列表,并提供了三种不同的实现方法,通过选择属性、选择对象的方式展示了如何在网页中动态地进行数据过滤和选项更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法一:选择属性

<!DOCTYPE html>
<html ng-app="my_app">
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript" src="jquery-1.10.2.min.js"></script>
		<script type="text/javascript" src="angular.min-1.69.js"></script>
		<script type="text/javascript">
			var app = angular.module("my_app", []);
			app.controller('my_controller', function($scope) {
				window.scope = $scope;
				$scope.selectedClass = '';
				$scope.selectedStudent = '';
				/* 班级信息 */
				$scope.classes = [{
					classno: 'c001',
					classname: '1班'
				}, {
					classno: 'c002',
					classname: '2班'
				}];
				/* 学生信息 */
				$scope.students = [{
					studentno: 's001',
					studentname: '张三',
					classno: 'c001',
				}, {
					studentno: 's002',
					studentname: '李四',
					classno: 'c001',
				}, {
					studentno: 's003',
					studentname: '王五',
					classno: 'c002',
				}, {
					studentno: 's004',
					studentname: '马六',
					classno: 'c002',
				}];
				$scope.filterStudents=[];
				$scope.classChange=function(){
					$scope.selectedStudent = '';
					$scope.filterStudents=[];
					$.each($scope.students,function(i,item){
						if(item.classno==$scope.selectedClass){
							$scope.filterStudents.push(item);
						}
					});
				}
				$scope.get = function() {
					alert($scope.selectedClass + ',' + $scope.selectedStudent);
				}
			});
		</script>
	</head>
	<body ng-controller="my_controller">
		<div>
			<select ng-model='selectedClass' ng-change="classChange();">
				<option value="">请选择</option>
				<option ng-repeat="classItem in classes" 
				value="{{classItem.classno}}">{{classItem.classname}}
				</option>
			</select>
			<select ng-model='selectedStudent'>
				<option value="">请选择</option>
				<option ng-repeat="studentItem in filterStudents"
				value="{{studentItem.studentno}}">{{studentItem.studentname}}
				</option>
			</select>
			<input type="button" ng-click="get();" value="获取" />
		</div>
</html>

方法二:选择对象

<!DOCTYPE html>
<html ng-app="my_app">
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript" src="jquery-1.10.2.min.js"></script>
		<script type="text/javascript" src="angular.min-1.69.js"></script>
		<script type="text/javascript">
			var app = angular.module("my_app", []);
			app.controller('my_controller', function($scope) {
				window.scope = $scope;
				/* 班级、学生信息 */
				$scope.classes = [{
						classitem: {
							classno: 'c001',
							classname: '1班'
						},
						students: [{
							studentno: 's001',
							studentname: '张三',
						}, {
							studentno: 's002',
							studentname: '李四',
						}]
					},
					{
						classitem: {
							classno: 'c002',
							classname: '2班'
						},
						students: [{
							studentno: 's003',
							studentname: '王五',
						}, {
							studentno: 's004',
							studentname: '马六',
						}]
					},
					{
						classitem: {
							classno: 'c003',
							classname: '3班'
						},
						students: []
					}
				]
				$scope.selectedClass = $scope.classes[0];
				$scope.selectedStudent = {};
				if ($scope.selectedClass.students.length > 0) {
					$scope.selectedStudent = $scope.selectedClass.students[0];
				}
				$scope.classChange = function() {
					$scope.selectedStudent = {};
					if ($scope.selectedClass.students.length > 0) {
						$scope.selectedStudent = $scope.selectedClass.students[0];
					}
				}
				$scope.get = function() {
					var resultclassno = $scope.selectedClass.classitem.classno;
					var resultstudentno = $scope.selectedStudent.studentno == undefined 
					? '' : $scope.selectedStudent.studentno
					alert(resultclassno + ',' + resultstudentno);
				}
			});
		</script>
	</head>
	<body ng-controller="my_controller">
		<div>
			<select ng-model="selectedClass" 
			ng-options="item.classitem.classname for item in classes" 
			ng-change="classChange();"></select>
			<select ng-model="selectedStudent" 
			ng-options="item.studentname for item in selectedClass.students"></select>
			<input type="button" ng-click="get();" value="获取" />
		</div>
</html>

方法3:选择对象

<!DOCTYPE html>
<html ng-app="my_app">
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript" src="jquery-1.10.2.min.js"></script>
		<script type="text/javascript" src="angular.min-1.69.js"></script>
		<script type="text/javascript">
			var app = angular.module("my_app", []);
			app.controller('my_controller', function($scope) {
				window.scope = $scope;
				/* 班级、学生信息 */
				$scope.classes = [{
						classno: 'c001',
						classname: '1班',
						students: [{
							studentno: 's001',
							studentname: '张三',
						}, {
							studentno: 's002',
							studentname: '李四',
						}]
					},
					{
						classno: 'c002',
						classname: '2班',
						students: [{
							studentno: 's003',
							studentname: '王五',
						}, {
							studentno: 's004',
							studentname: '马六',
						}]
					},
					{
						classno: 'c003',
						classname: '3班',
						students: []
					}
				]
				$scope.selectedClass = $scope.classes[0];
				$scope.selectedStudent = {};
				if ($scope.selectedClass.students.length > 0) {
					$scope.selectedStudent = $scope.selectedClass.students[0];
				}
				$scope.classChange = function() {
					$scope.selectedStudent = {};
					if ($scope.selectedClass.students.length > 0) {
						$scope.selectedStudent = $scope.selectedClass.students[0];
					}
				}
				$scope.get = function() {
					var resultclassno = $scope.selectedClass.classno;
					var resultstudentno = $scope.selectedStudent.studentno == undefined ?
						'' : $scope.selectedStudent.studentno
					alert(resultclassno + ',' + resultstudentno);
				}
			});
		</script>
	</head>
	<body ng-controller="my_controller">
		<div>
			<select ng-model="selectedClass" 
			ng-options="item.classname for item in classes" 
			ng-change="classChange();"></select>
			<select ng-model="selectedStudent" 
			ng-options="item.studentname for item in selectedClass.students"></select>
			<input type="button" ng-click="get();" value="获取" />
		</div>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值