重新加载时重置模态框上的值

时间:2016-02-25 07:49:19

标签: angularjs bootstrap-modal

重新加载时重置模态框上的值

fwrite()

这是模态框。

点击打开模态后,将调用此函数

<div class="modal fade" id="detailsModal" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
   <div class="modal-dialog">
      <div class="modal-content">
         <!-- Modal Header -->
         <div class="modal-header custom-modal-header">
            <button type="button" class="close" data-dismiss="modal">
            <span aria-hidden="true">&times;</span>
            <span class="sr-only">Close</span>
            </button>
            <h4 class="modal-title" id="myModalLabel">
               Create Job Vaccination
            </h4>
         </div>
         <!-- Modal Body -->
         <form name="vaccjobctrl.vaccineForm" class="form-inline" >
            <div class="modal-body" style="height: 150px">
               <div class="row ">
                  <div class="form-group col-md-12"  ng-class="{ 'has-error' : vaccjobctrl.vaccineForm.screeningType.$invalid && (vaccjobctrl.vaccineForm.screeningType.$dirty || vaccjobctrl.vaccineForm.screeningType.$touched) }">
                     <label for="regId" class="col-md-6">Screening Type:</label>
                     <span class="col-md-6">
                        <select style="width: 100%;"  
                           ng-model="vaccjobctrl.vaccineForm.screeningTypeMast.screeningTypeId" 
                           ng-options="sctype.screeningTypeId as sctype.screeningType for sctype in vaccjobctrl.screeningTypeList"
                           class="form-control field-size ng-pristine ng-invalid ng-invalid-required ng-valid-maxlength ng-touched" name="screeningType"
                           id="screeningType" required>
                           <option value="">--SELECT--</option>
                           <option value="{{sctype.screeningTypeId}}">{{sctype.screeningType}}</option>
                        </select>
                     </span>
                     <div class="col-sm-6 error-color"
                        ng-if="vaccjobctrl.interacted(vaccjobctrl.vaccineForm.screeningType)"
                        ng-messages="vaccjobctrl.vaccineForm.screeningType.$error">
                        <div ng-messages-include="src/common/validation-messages.html"></div>
                     </div>
                  </div>
               </div>

            <!-- Modal Footer -->
            <div class="modal-footer">
               <button type="button" class="btn btn-default" data-dismiss="modal">
               Cancel
               </button>
               <button type="button" class="btn btn-primary-joli"  ng-click="vaccjobctrl.saveJobVaccination(vaccjobctrl.vacc);" ng-disabled="vaccjobctrl.vaccineForm.$invalid" data-dismiss="modal">
               Create Vaccination
               </button>
            </div>
         </form>
      </div>
   </div>
</div>
选择框中的

值已预先填充,重新打开模式框时会显示先前选择的值。如果尝试清除重新打开时选择框边框颜色变为红色。

1 个答案:

答案 0 :(得分:0)

Bootstrap 3中,您可以在模态窗口关闭后重置您的表单,如下所示:

$('.modal').on('hidden.bs.modal', function(){
    $(this).find('form')[0].reset();
});

隐藏模态时,您可以手动重置任何内容。

<强> HTML:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>

<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch modal
</button>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
        </button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        <div class='modal-body1'>
          <h3>Close and open, I will be gone!</h3>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

您可以使用:

$(".modal").on("hidden.bs.modal", function(){
    $(".modal-body1").html("");
});

你可以做更多的事情:

$(document).ready(function() {
  $(".modal").on("hidden.bs.modal", function() {
    $(".modal-body1").html("Where did he go?!?!?!");
  });
});

http://getbootstrap.com/javascript/#modals-usage中有更多关于它们的内容。

根据您的需要更新:

如果您想angular方式,请使用index.html angularjs source 上方的 jquery src 。您只需使用jquery控制器内的angularjs函数即可。

工作JsFiddle:http://jsfiddle.net/tnq86/15/

在angular。中使用document.ready函数。

 angular.module('MyApp', [])

.controller('MyCtrl', [function() {
    angular.element(document).ready(function () {
        document.getElementById('msg').innerHTML = 'Hello';
    });
}]);

但是,创建指令是正确的方法。请点击此链接,获取有关创建指令以在jquery中执行angularjs功能的帮助。

jquery in angularjs using directive