23

我将如何在表单中添加功能,以便用户可以通过单击“添加”添加更多输入字段。这使用角度形式库。

这是一个确切功能的示例,但仅使用 angularjs 完成。

动态添加表单域

4

3 回答 3

16

看到这个Plunker

这是您需要的示例。正如您在 plunker 中看到的那样,有一个TextArea可以在按钮单击时动态创建。创建的TextAreas也可以通过remove单击按钮删除。

请参阅下面的HTML

<div class="col-sm-10">
  <input type="button" class="btn btn-info" ng-click="addNewChoice()" value="ADD QUESTION">
  <div class="col-sm-4">
    <fieldset data-ng-repeat="field in choiceSet.choices track by $index">
      <textarea rows="4" cols="50" ng-model=" choiceSet.choices[$index]"></textarea>
      <button type="button" class="btn btn-default btn-sm" ng-click="removeChoice($index)">
        <span class="glyphicon glyphicon-minus"></span> REMOVE
      </button>
    </fieldset>
  </div>
</div>

JS如下_

var app = angular.module('myApp', []);
app.controller('inspectionController', function($scope, $http) {
  $scope.choiceSet = {
    choices: []
  };
  $scope.quest = {};
  $scope.choiceSet.choices = [];
  $scope.addNewChoice = function() {
    $scope.choiceSet.choices.push('');
  };
  $scope.removeChoice = function(z) {
    $scope.choiceSet.choices.splice(z, 1);
  };
});
于 2016-02-20T19:06:43.247 回答
11

我举个简单的例子。

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

app.controller("MyCtrl" , function($scope){
  
   $scope.data ={
       names:[{ name:""}]
   };
  
  $scope.addRow = function(index){
    var name = {name:""};
       if($scope.data.names.length <= index+1){
            $scope.data.names.splice(index+1,0,name);
        }
    };
  
  $scope.deleteRow = function($event,name){
  var index = $scope.data.names.indexOf(name);
    if($event.which == 1)
       $scope.data.names.splice(index,1);
    }
  
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="MyCtrl">
   <table>
     <tr ng-repeat="name in data.names track by $index">
        <td> <input type="text" ng-model="data.names[$index].name"></td>
        <td> <input type="button" ng-click="addRow($index)" value="Add" ng-show="$last"></td>
        <td> <input type="button" ng-click="deleteRow($event,name)" value="Delete" ng-show="$index != 0"></td>
     </tr>
   </table> 
    <span>{{data|json}}</span>
</div>

于 2016-02-24T13:07:38.660 回答
0

简单的示例也可以按任何顺序删除:

http://plnkr.co/edit/c0FbjBJkHtDYRKslz0iq?p=preview

或者:

一些html:

<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
    <script src="script.js"></script>
  </head>

  <body ng-app="oneApp" ng-controller="ctrl">
    <button ng-click="addNewRow()">Add row</button>

    <table>
        <tr ng-repeat="row in tablerows track by $id(row)">
        <td ng-model="tablerows[$index]">
                <input>
            </td>
            <td>
                <button ng-click="removeRow($index)" type="button">
                  Delete
                </button>
            <td>
        </tr>
    </table>
  </body>
</html>

脚本.js:

var app = angular.module('oneApp', []);
app.controller('ctrl', function($scope){
    $scope.tablerows = [];

    $scope.addNewRow = function () {
        var row_id;
        var tablerows = $scope.tablerows;
        if(tablerows.length > 0){
            row_id = tablerows[tablerows.length-1];
            row_id = row_id +1;
        }else{
            row_id = 0;
        }
        $scope.tablerows.push(row_id);
    };

    $scope.removeRow = function (index) {
        $scope.tablerows.splice(index,1);
    };
    }
);
于 2017-12-22T05:45:49.643 回答