Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically add input form?

Tags:

angularjs

I am trying to dynamically add input forms when "New Item" is clicked. But not really know how to write the code in the js part. Any idea?

Here is my code:

<!doctype html>
<html ng-app>
  <head>
    <title>Angular - My Notes</title>
    <link rel="stylesheet" type="text/css" href="css/index.css">

  <body>
    <h1>My Notes</h1>
    <div ng-controller="Note">
      <input type="text" placeholder="Question">
      <input ng-class="{'blockInput': !inlineChecked}" type="text" placeholder="enter text...">
      <input type="checkbox" name="check" value="inline" ng-model="inlineChecked"> Inline
      <br>
      <button ng-click="add()">New Item</button>
    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script>
    <script type="text/javascript">

      var Note = function($scope){
        // create a variable contain new input forms?? 
      }


    </script>
  </body>
</html>
like image 559
Jusleong Avatar asked Feb 27 '14 21:02

Jusleong


People also ask

How do you add inputs dynamically?

Create a button and on clicking this button we will dynamically add the input fields. Now write the click() function to handle the adding and removing functionality. Use the append() method to add the input field code to the existing HTML document.

How do you dynamically submit a form?

Answer: Use the jQuery submit() Method You can use the submit() method to submit an HTML form (i.e. ) using jQuery. The jQuery code in the following example will submit the form on click of the button (i.e. the element) which has the type attribute set to button (i.e. type=”button” ).

What is dynamic input field?

Dynamic input field allows providing multiple values in a form. It is very useful when you want to receive multiple inputs for the same field in an HTML form. The dynamic input fields feature can be easily integrated using jQuery. You can add multiple fields and remove fields easily.


2 Answers

Use an array and ngRepeat...

<!doctype html>
<html ng-app>
  <head>
    <title>Angular - My Notes</title>

  <body>
    <h1>My Notes</h1>
    <div ng-controller="Note">
      <div ng-repeat="item in items">
        <input type="text" placeholder="{{item.questionPlaceholder}}" ng-model="item.question">
        <input ng-class="{'blockInput': !item.inlineChecked}" type="text" placeholder="enter text..." ng-model="item.text">
        <input type="checkbox" name="check" value="inline" ng-model="item.inlineChecked"> Inline
      </div>
      <button ng-click="add()">New Item</button>
    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script>
    <script type="text/javascript">

      var Note = function($scope){
        $scope.items = [];

        $scope.add = function () {
          $scope.items.push({ 
            inlineChecked: false,
            question: "",
            questionPlaceholder: "foo",
            text: ""
          });
        };
      }


    </script>
  </body>
</html>

JsBin... http://jsbin.com/fusapojo/4/edit?html,output

like image 123
Anthony Chu Avatar answered Sep 24 '22 11:09

Anthony Chu


HTML:

<div ng-repeat="flavour in flavours" class="form-group">
    <label class="col-sm-1 control-label">Name</label>
    <div class="col-sm-2">
        <div class="input-group">
                <input type="text" class="form-control" placeholder="" ng-model="flavour.name">
        </div>                
    </div>
</div>

JS: $scope.flavours = [];

$scope.addFlavour = function () {
    $scope.flavours.push({ 
        name: "",
        pg: 100,
        vg: 0,
        quantity: 5.0
    });
}
like image 28
Luiz Picanço Avatar answered Sep 22 '22 11:09

Luiz Picanço