jquery 关闭引导模态后重置验证消息-Laravel 9

omtl5h9j  于 2022-12-03  发布在  jQuery
关注(0)|答案(2)|浏览(117)

我有一个带输入的模态,我使用Bootstrp模态和jqeury一些验证代码在提交时验证我的表单,我的问题是,当我关闭模态,然后再次打开模态时,来自验证的消息仍然存在,所以我的问题是,当我关闭模态时,我如何重置或隐藏这些消息?

下面是javascript代码

$(document).ready(function() {         
      $('#addForm').validate({
          rules: {
              name: {
                  required: true,
              },
              mobile_no: {
                  required: true,
              },
              address: {
                  required: true,
              },
              email_address: {
                  required: true,
              },
          },
          messages: {
              name: {
                  required: 'Please Enter Supplier Name',
              },
              mobile_no: {
                  required: 'Please Enter Supplier mobile number',
              },
              address: {
                  required: 'Please Enter Supplier address',
              },
              email_address: {
                  required: 'Please Enter Supplier email',
              },
          },
          errorElement: 'span',
          errorPlacement: function(error, element) {
              error.addClass('invalid-feedback');
              element.closest('.form-group').append(error);
          },
          highlight: function(element, errorClass, validClass) {
              $(element).addClass('is-invalid');
          },
          unhighlight: function(element, errorClass, validClass) {
              $(element).removeClass('is-invalid');
          },
      });
  });

以下是引导模式中的代码

<!-- Modal -->
  <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
      aria-hidden="true">
      <div class="modal-dialog" role="document">
          <div class="modal-content">
              <div class="modal-header">
                  <h5 class="modal-title" id="exampleModalLabel">Add Supplier</h5>
                  <button type="button" class=" btn btn-danger btn btn-sm close" data-dismiss="modal"
                      aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                  </button>
              </div>
              <form id="addForm" method="POST" action="{{ route('supplier.store') }}">
                  @csrf

                  <div class="modal-body">
                      <!-- name -->
                      <div class="col-md-12 ">
                          <div class="mb-3 position-relative form-group">

                              <input class="form-control" type="text" autocomplete="name" placeholder="Supplier Name"
                                  id="name" name="name" value="">
                          </div>

                      </div>

                      <!-- mobile number -->
                      <div class="col-md-12 ">
                          <div class="mb-3 position-relative form-group">

                              <input class="form-control " type="number" autocomplete="mobile_no"
                                  placeholder="Mobile Number" id="mobile_no" name="mobile_no" value="">

                          </div>
                      </div>
                      <!-- email -->
                      <div class="col-md-12 ">
                          <div class="mb-3 position-relative form-group">
                              <input class="form-control " type="email" placeholder="Email" id="email_address"
                                  name="email_address" value="">

                          </div>
                      </div>
                      <div class="col-md-12 ">
                          <div class="mb-3 position-relative form-group">
                              <input class="form-control" type="text" autocomplete="address" placeholder="Address"
                                  id="address" name="address" value="">

                          </div>
                      </div>

                  </div>
                  <div class="modal-footer">
                      <button type="button" class="btn btn-secondary close" data-dismiss="modal"
                          onclick="a()">Close</button>
                      <button type="submit" class="btn btn-primary">Add
                          Supplier</button>
                  </div>
              </form>
          </div>
      </div>
  </div>

index.php中的代码

<div style="float: right"><button type="button " class="btn btn-primary" data-toggle="modal"
data-target="#exampleModal" >Add Supplier</button></div><br><br>
41zrol4v

41zrol4v1#

$('#exampleModal').on('hide.bs.modal', function () {
  // Remove error messages
})

有关模式事件的详细信息:https://getbootstrap.com/docs/4.0/components/modal/#events

wmomyfyw

wmomyfyw2#

我找到了解决办法
我创建函数resetForm,如下所示,借助此链接enter link description here

function resetForm() {
          $("#addForm input[type='text'],input[type='email'],input[type='number']").each(function() {
              $(this).removeClass('form-control input-validation-error');
              $(this).removeClass('form-control invalid-feedback');
              $(this).removeClass('form-control is-invalid');
              $(this).addClass("form-control");
          });
          $(".field-validation-error").text('');
      }

并在按钮中调用它,如下所示

<button type="submit" name="reset" id="reset" class="btn btn-secondary close"
                          onclick="resetForm()" data-dismiss="modal">Close</button>

相关问题