简体   繁体   中英

Angularjs - ng-submit not working in form

I'm new to AngularJS and I'm trying to submit a simple form using ng-submit but the button not working as well as it's not clickable, It's working on Firefox but the cursor still not changing when it's on the button and not working on Chrome.

HTML

<form novalidate name="newTripForm" ng-submit="vm.addTrip()">
      <div class="form-group">
          <label for="name">Trip Name</label>
          <input type="text" class="form-control" id="name" name="name"  ng-model="vm.newTrip.name"/>
      </div>
      <div class="form-group">
           <input type="submit"  class="btn btn-sm btn-success" id="submit" value="Add"/>
      </div>  
</form>

JavaScript

 vm.addTrip = function ()
    {
        alert(vm.newTrip.name);
    };

try this:

<form novalidate name="newTripForm" ng-submit="vm.addTrip()">
  <div class="form-group" ng-init="vm.newTrip.name = '';">
      <label for="name">Trip Name</label>
      <input type="text" class="form-control" id="name" name="name"  ng-model="vm.newTrip.name" required/>
  </div>
  <div class="form-group">
       <input type="submit"  class="btn btn-sm btn-success" id="submit" value="Add"/>
  </div>  

Based on and AngularJS docs , you can have two submits on your form.

I created a quick sample based of what is on the docs and your example.

 (function() { var app = angular.module("test", []); var TestController = function() { var vm = this; vm.newTrip = { name: "" }; vm.addTrip = function() { // Some code console.log("Added " + vm.newTrip.name + " to database!"); vm.newTrip.name = ""; } } app.controller("TestController", [TestController]); })(); 
 <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> </head> <body ng-app="test"> <div ng-controller="TestController as vm"> <form novalidate name="newTripForm" ng-submit="vm.addTrip()"> <div class="form-group"> <label for="name">Trip Name</label> <input type="text" class="form-control" id="name" name="name" ng-model="vm.newTrip.name" /> </div> <div class="form-group"> <input type="submit" class="btn btn-sm btn-success" id="submit" value="Add" /> </div> </form> </div> </body> </html> 

try this remove the form

<div class="form-group">
          <label for="name">Trip Name</label>
          <input type="text" class="form-control" id="name" name="name"  ng-model="vm.newTrip.name"/>
      </div>
      <div class="form-group">
           <button ng-click="addTrip()"></button>
      </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM