AngularJS实现的省市二级联动功能示例【可对选项实现增删】
程序员文章站
2022-03-20 21:58:41
本文实例讲述了angularjs实现的省市二级联动功能。分享给大家供大家参考,具体如下:
...
本文实例讲述了angularjs实现的省市二级联动功能。分享给大家供大家参考,具体如下:
<!doctype html> <html> <head lang="en"> <meta charset="utf-8"> <title>www.jb51.net 省市二级联动</title> </head> <style> *{ margin:0; padding:0; } .ul{ list-style:none; display:inline-block; } .selectoption ul li{ display:inline-block; width:50px; padding:5px; } ul li:hover{ background-color:#59c0f3; color:#fff; cursor:pointer; } .container{ display:inline-block; } dl{ display: inline-block; } dl dt{ display: inline-block; } .container{ position:relative; text-align: left; vertical-align:top; } .selectboder{ width:150px; height:20px; margin:0 auto; margin-bottom:5px; cursor:pointer; border:1px solid #808080; padding:2px 5px; font-size:14px; } .selectboder:hover{ border-color:#59c0f3; } .selectoption:before{ width: 0; height: 0; border-bottom: 50px solid #ffffff; border-left: 50px solid transparent; border-right: 50px solid transparent; } .selectoption{ font-size:14px; position:absolute; background-color: #ffffff; z-index:9999; border:1px solid #eee; width:360px; padding:3px 5px; box-shadow: 5px 5px 10px #888888; } .button{ width:30px; height:30px; display: inline-block; background-color:#59c0f3; text-align:center; line-height: 25px; cursor:pointer; font-size:24px; color:#fff; margin:0 5px; border-radius:30px; } .button:hover{ background-color:#12bb16; } </style> <body ng-app="myapp" ng-controller="mycontrol"> <div class="inline" ng-repeat = "option in options"> <div class="container"> <dl> <dd class="selectboder" ng-click="chosep($index)" ng-value="false">{{option.province}}</dd> <dt class="selectoption" ng-if="option.ifshowprovince" ng-mouseleave="leaveprovince(option)"> <ul> <li ng-repeat = " x in province" ng-value="x.value" ng-click="choseprovince($event.target,option)" data-name="{{x.name}}">{{x.name}}</li> </ul> </dt> </dl> </div> <div class="container"> <dl> <dd class="selectboder" ng-click="chosec($index)" ng-value="false">{{option.city}}</dd> <dt class="selectoption" ng-if="option.ifshowcity" ng-mouseleave="leavecity(option)"> <ul> <li ng-repeat = "y in option.cities" ng-value="y.value" ng-click="chosecity($event.target,option)" data-name="{{y.name}}">{{y.name}}</li> </ul> </dt> </dl> </div> <span class="button" ng-click="addchose($index)">+</span> <span class="button" ng-click="deletechose($index)">-</span> </div> </div> </body> <script type="text/javascript" src="../js/jquery-1.8.3.js"></script> <script type="text/javascript" src="../js/angular.min.js"></script> <script type="text/javascript"> var app = angular.module('myapp',[]); app.controller('mycontrol',function($scope){ $scope.ifshowcity = false; $scope.ifshowprovince = false; $scope.options =[{index:"0",ifshowcity:false,ifshowprovince:false,province:"",city:"",cities:""}]; $scope.leaveprovince = function(option){ $.each($scope.options,function(index,item){ if(option == $scope.options[index]){ $scope.optionindex = index; } }) $scope.options[$scope.optionindex].ifshowprovince = false; } $scope.leavecity = function(option){ $.each($scope.options,function(index,item){ if(option == $scope.options[index]){ $scope.optionindex = index; } }) $scope.options[$scope.optionindex].ifshowcity = false; } $scope.choseprovince = function(target,option){ $.each($scope.options,function(index,item){ if(option == $scope.options[index]){ $scope.optionindex = index; } }) $scope.options[$scope.optionindex].ifshowprovince = false; $scope.options[$scope.optionindex].province = target.getattribute("data-name"); $.each($scope.province,function(index,item){ if(item.value == target.getattribute("value")){ $scope.options[$scope.optionindex].cities = item.children; } } ) } $scope.chosecity = function(target,option){ $.each($scope.options,function(index,item){ if(option == $scope.options[index]){ $scope.optionindex = index; } }) $scope.options[$scope.optionindex].ifshowcity = false; $scope.options[$scope.optionindex].city = target.getattribute("data-name"); } function getprarms(){ return $scope.options; } $scope.sub = function(){ getprarms(); } $scope.province = [{ name: "湖北省", value: "01", children: [{ name: "武汉", value: "0101" }, { name: "黄冈", value: "0102" }, { name: "荆州", value: "0103" }, { name: "十堰", value: "0104" }, { name: "黄石", value: "0105" }, { name: "鄂州", value: "0106" }, { name: "咸宁市", value: "0107" }, { name: "襄阳市", value: "0108" } ] },{ name: "广东省", value: "02", children: [{ name: "广东", value: "0201" }, { name: "深圳", value: "0202" }, { name: "佛山", value: "0203" }, { name: "惠州", value: "0204" }, { name: "东莞", value: "0205" }] },{ name: "河北省", value: "03", children: [{ name: "北京", value: "0301" }, { name: "邯郸", value: "0302" }, { name: "邢台", value: "0303" }, { name: "保定", value: "0304" }, { name: "秦皇岛", value: "0305" } ]} ] $scope.chosep = function($index){ $scope.options[$index].ifshowprovince = !$scope.options[$index].ifshowprovince; $scope.options[$index].ifshowcity = false; } $scope.chosec = function($index){ $scope.options[$index].ifshowcity = !$scope.options[$index].ifshowcity; $scope.options[$index].ifshowprovince = false; } $scope.addchose = function($index){ if($scope.options.length < 10){ $scope.options.splice($scope.options.length,0,{ //从最后面添加内容 index:$scope.options.length,ifshowcity:false,ifshowprovince:false }); $scope.candelete = true; }else{ $scope.canadd = false; } } $scope.deletechose = function($index){ if($scope.options.length >1){ $scope.options.splice($index,1); //从当前行删除。 } if($index == 1){ $scope.candelete = false; } } }); </script> </html>
更多关于angularjs相关内容感兴趣的读者可查看本站专题:《angularjs指令操作技巧总结》、《angularjs入门与进阶教程》及《angularjs mvc架构总结》
希望本文所述对大家angularjs程序设计有所帮助。
上一篇: ClickHouse 安装(参考)
下一篇: 重学HTML5的语义化