在提交表单之前显示弹出窗口/模式以进行确认

Posted

技术标签:

【中文标题】在提交表单之前显示弹出窗口/模式以进行确认【英文标题】:Show pop-up/modal for confirmation before form submission 【发布时间】:2017-05-18 05:53:02 【问题描述】:

使用 AngularJS,我们如何在提交表单数据之前显示一个弹出窗口,用于重新检查和确认单击提交按钮时输入的输入值?

【问题讨论】:

我猜你在寻找什么,就 UI 而言,被称为“模态”?你可以在这里找到一些例子angular-ui.github.io/bootstrap搜索模态。 【参考方案1】:

试试Uib modal。 https://angular-ui.github.io/bootstrap/#!#modal

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl', function ($uibModal, $log, $document) 
  var $ctrl = this;
  $ctrl.animationsEnabled = true;
  $ctrl.name = "Manikandan";

  $ctrl.open = function (size, parentSelector) 
    var parentElem = parentSelector ? 
      angular.element($document[0].querySelector('.modal-demo ' + parentSelector)) : undefined;
    var modalInstance = $uibModal.open(
      animation: $ctrl.animationsEnabled,
      ariaLabelledBy: 'modal-title',
      ariaDescribedBy: 'modal-body',
      templateUrl: 'myModalContent.html',
      controller: 'ModalInstanceCtrl',
      controllerAs: '$ctrl',
      size: size,
      appendTo: parentElem,
      resolve: 
        values: function () 
          return $ctrl.name;
        
      
    );

    modalInstance.result.then(function () 
      $ctrl.msg = "Submitted";
      $ctrl.suc = true; 
    , function(error) 
      $ctrl.msg = 'Cancelled';
      $ctrl.suc = false; 
    );
  ;
);


angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($uibModalInstance, values) 
 var $ctrl = this;
 $ctrl.name= values;
  $ctrl.ok = function () 
    $uibModalInstance.close('ok');
  ;

  $ctrl.cancel = function () 
    $uibModalInstance.dismiss('cancel');
  ;
);
<!doctype html>
<html ng-app="ui.bootstrap.demo">
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-animate.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-sanitize.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.5.0.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>

<div ng-controller="ModalDemoCtrl as $ctrl" class="modal-demo">
    
   <br> 
<form name="form" novalidate>
  <input type="text" style="width:200px" class="form-control" name="name" ng-model="$ctrl.name" required><br>
   <button type="button" ng-disabled="form.$invalid" class="btn btn-default" ng-click="form.$valid && $ctrl.open()">Save</button>
</form><br>
    <p ng-hide="!$ctrl.msg" class="alert" ng-class="'alert-success':$ctrl.suc, 'alert-danger':!$ctrl.suc">$ctrl.msg</p>
    
</div>
<script type="text/ng-template" id="myModalContent.html">
        <div class="modal-header">
            <h3 class="modal-title" id="modal-title">Your Details</h3>
        </div>
        <div class="modal-body" id="modal-body">
           
            <p>Are you sure, your name <b>$ctrl.name </b> is going to submit?
        </div>
        <div class="modal-footer">
            <button class="btn btn-primary" type="button" ng-click="$ctrl.ok()">Submit</button>
            <button class="btn btn-warning" type="button" ng-click="$ctrl.cancel()">Cancel</button>
        </div>
    </script>
  </body>
</html>

【讨论】:

【参考方案2】:

用js代码:

if(confirm("xxx"))
 
      //do next step
 

【讨论】:

以上是关于在提交表单之前显示弹出窗口/模式以进行确认的主要内容,如果未能解决你的问题,请参考以下文章

在提交实际表单之前接受弹出窗口中的输入

使用 jquery UI 确认表单提交

netbeans chrome“确认表单重新提交”

弹出窗口Session丢失防止表单重复提交问题

联系表单 7 成功提交后如何打开弹窗

如何在使用 jquery confirm 提交表单之前显示确认框?