1. 程式人生 > >文章標題 複選框 點選獲取文字內容

文章標題 複選框 點選獲取文字內容

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
<script src="angular.js"></script>



    <script>
        var app = angular.module("myApp",[]);
        app.controller("myCtrl",function($scope){
            $scope.data = [{
                key:"1",
                value:"這是第一項"
            },{
                key:"2",
                value:"這是第二項"
            },{
                key:"3",
                value:"這是第三項"
            },{
                key:"4",
                value:"這是第四項"
            }];


            $scope.arr = [];
            $scope.fun = function(flag,text){
                //alert(flag);
                if(flag){
                    //alert("asf");
                    $scope.arr.push(text);
                }else{
                    for(i in $scope.arr){
                        if($scope.arr[i] == text){
                            $scope.arr.splice(i,1);
                        }
                    }
                }
                //alert($scope.arr.length);
            }

            //確認按鈕的點選事件
            $scope.check = function(){
                if($scope.arr.length==0){
                    alert("請先選擇");
                }else{
                    alert($scope.arr);
                }
            }
        });
    </script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
<center>
    <button ng-click="check()">確認</button>
    <ul ng-repeat="i in data">
        <input type="checkbox"
               name="check"
               ng-value="i.key"
               ng-model="flag"
               ng-click="fun(flag,i.key)"
        />
        {{i.value}}
    </ul>
</center>
</body>
</html>