加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 如何获取多个选中的复选框值

发布时间:2020-12-17 17:38:30 所属栏目:安全 来源:网络整理
导读:如何在Angular.js中获取多个选中的复选框值并插入到用逗号(,)分隔的数据库中? 解决方法 您可以在一系列选项中使用ng-repeat ng-model. DEMO http://plnkr.co/edit/KcUshc74FZL1npZsKbEO?p=preview HTML body ng-controller="MainCtrl" div class="container
如何在Angular.js中获取多个选中的复选框值并插入到用逗号(,)分隔的数据库中?

解决方法

您可以在一系列选项中使用ng-repeat ng-model.

DEMO http://plnkr.co/edit/KcUshc74FZL1npZsKbEO?p=preview

HTML

<body ng-controller="MainCtrl">
  <div class="container">

    <h1>Options</h1>

    <div>

      <div class="form-group" ng-repeat="option in options">
        <label>{{option.name}}</label>
        <input type="checkbox" ng-model="option.value">
      </div>

    </div>

    <hr>
    <button class="btn btn-primary" ng-click="save()">save to database</button>

  </div>


</body>

JS

var app = angular.module('plunker',[]);

app.controller('MainCtrl',function($scope) {

  $scope.options = [{
    name: 'java',value: true,},{
    name: 'c#',value: false
  },{
    name: 'angular',value: true
  },{
    name: 'r',{
    name: 'python',{
    name: 'c++',value: true
  }];

  $scope.save = function() {

    var optionsCSV = '';

    $scope.options.forEach(function(option) {

      if (option.value) {

        // If this is not the first item
        if (optionsCSV) {
          optionsCSV += ','
        }
        optionsCSV += option.name;
      }

    })

    // Save the csv to your db (replace alert with your code)
    alert(optionsCSV);

  };


});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读