angular.js中一组复选框,怎么实现选中其中一个,其他不能选?
大家讲道理
大家讲道理 2017-05-15 17:04:00
0
3
676

angular.js中一组复选框,怎么实现选中其中一个,其他不能选?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

membalas semua(3)
伊谢尔伦
<!DOCTYPE html>
<html lang="en" ng-app="app">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <p ng-controller="demo">
        <p>当前值: {{hobby}}</p>
        <ul>
            <li ng-repeat="i in [1,2,3]">
                <label>吃:</label>
                <input type="checkbox" ng-click="check(i)" ng-disabled="hobby == i" ng-checked="hobby == i" />
            </li>
        </ul>
    </p>

    <script src="http://cdn.bootcss.com/angular.js/1.5.7/angular.min.js"></script>

    <script>
        angular.module('app', []).controller('demo', ['$scope', function ($scope) {
            $scope.hobby = 1;

            $scope.check = function (i) {
                $scope.hobby = i;
            }
        }]);
    </script>
</body>
</html>
洪涛

ng-modelKetekalan diperlukan. Gunakan nilai untuk mengawal kandungan.

<script>
  angular.module('radioExample', [])
    .controller('ExampleController', ['$scope', function($scope) {
      $scope.color = {
        name: 'blue'
      };
      $scope.specialValue = {
        "id": "12345",
        "value": "green"
      };
    }]);
</script>
<form name="myForm" ng-controller="ExampleController">
  <label>
    <input type="radio" ng-model="color.name" value="red">
    Red
  </label><br/>
  <label>
    <input type="radio" ng-model="color.name" ng-value="specialValue">
    Green
  </label><br/>
  <label>
    <input type="radio" ng-model="color.name" value="blue">
    Blue
  </label><br/>
  <tt>color = {{color.name | json}}</tt><br/>
 </form>
 Note that `ng-value="specialValue"` sets radio item's value to be the value of `$scope.specialValue`.
大家讲道理

Kod berikut ialah idea Terdapat banyak masalah, contohnya, saya mendapati bahawa senarai itu tidak boleh diekstrak dan menggunakan ng-for tidak boleh dilaksanakan. .disabled memerlukan anda menyesuaikan gaya sendiri.


<!DOCTYPE html>
<html lang="en" ng-app="app">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link href="//cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <p ng-controller="demo">
        <p>当前值: {{hobby}}</p>
        <ul>
            <li>
                <label>吃:</label>
                <input type="checkbox" ng-true-value="1" ng-model="hobby" ng-class="{disabled: hobby}" ng-change="handleCheckbox($event)" />
            </li>
            <li>
                <label>睡:</label>
                <input type="checkbox" ng-true-value="2" ng-model="hobby" ng-class="{disabled: hobby}" ng-change="handleCheckbox($event)" />
            </li>
        </ul>
    </p>

    <script src="//cdn.bootcss.com/angular.js/1.5.7/angular.min.js"></script>
    <script>
        angular.module('app', [])
            .controller('demo', ['$scope', function ($scope) {
                $scope.hobby = false;
            }]);


    </script>
</body>
</html>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan