首頁  >  問答  >  主體

angular.js - 使用angular如何實作讓checkbox單選,只能選擇一個呢?

使用angular如何實作讓checkbox單選,只能選擇一個呢?

如圖,以下是table中的checkbox選項:

<td width="30" style="text-align: left"><input
                            type="checkbox" ng-model="checkboxes.items[item.bookingId]" name="flag" ng-click="updateSelection(item.bookingId,checkboxes.items)"/></td>

如果在物件的controller裡面實現,只能讓其單選,選擇一個呢? ?

習慣沉默習慣沉默2713 天前578

全部回覆(2)我來回復

  • 迷茫

    迷茫2017-05-15 17:05:18

    checkbox是複選框,單選請用radio

    回覆
    0
  • PHP中文网

    PHP中文网2017-05-15 17:05:18

    試試這個呢

    <p ng-controller="MyCtrl">
        <p ng-repeat="item in list">
            <label><input type="checkbox" ng-model="item.checked" ng-change="onChange(item)">{{item.name}}</label>
        </p>
    </p>
    .controller('MyCtrl', function ($scope) {
        $scope.selected = '';
        $scope.list = [
            {id: 1, name: 'xxx'},
            {id: 2, name: 'yyy'},
            {id: 3, name: 'zzz'}
        ];
    
        $scope.onChange = function (item) {
            if (item.checked) {
                if (!$scope.selected) $scope.selected = item;
                if ($scope.selected !== item) item.checked = false;
            } else {
                $scope.selected = '';
            }
        }
    });

    回覆
    0
  • 取消回覆