Bootstrap 无法通过JavaScript和AJAX显示/隐藏跨度微调器?

ncecgwcz  于 12个月前  发布在  Bootstrap
关注(0)|答案(1)|浏览(116)

我试图显示和隐藏一个联系人表单的提交按钮上的微调。当表单正在提交时,微调器应该显示在按钮上。一旦表单被提交,微调器应该被隐藏。
这是我的提交按钮沿着和微调器跨度:

<div class="d-flex justify-content-center">
  <button class="btn btn-medium btn-fancy btn-primary mb-0 ls-wider" type="submit" id="submitButton">
    <span class="submitSpinner spinner-border spinner-border-sm me-1"></span> Send Message
  </button>
</div>

字符串
这是我使用的AJAX函数:

$(document).ready(function() {
        $('.submitSpinner').css('display','none');
        $("#contact-form").submit(function(e) {
          e.preventDefault();
          $('#submitButton').text('Sending Message');
          $('.submitSpinner').css('display','inline-block');
          var data = {
            'name': $('#name').val(),
            'email': $('#email').val(),
            'phone': $('#phone').val(),
            'company': $('#company').val(),
            'message': $('#message').val(),
          }
          $.ajaxSetup({
            headers: {
              'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
          });
          $.ajax({
            type: "POST",
            url: "/contact/message/send",
            data: data,
            dataType: "json",
            success: function(response) {
              // console.log(response);
              if (response.status == 400) {
                $.each(response.errors, function(key, error) {
                  $('#contact-alerts').append('<div class="col-12 col-md-9 col-lg-7 alert alert-danger alert-dismissible fade in show" role="alert"><button type="button" class="btn-close line-height-unset" data-bs-dismiss="alert" aria-label="Close"></button>' + error + '</div>')
                });
                $('.submitSpinner').css('display','none');
              } else {
                $("#contact-form")[0].reset();
                $('#submitButton').text('Send Message');
                $('.submitSpinner').css('display','none');
                $.magnificPopup.open({
                  showCloseBtn: true,
                  items: {
                    src: '#contact-success'
                  },
                  type: 'inline',
                  fixedContentPos: false,
                  fixedBgPos: true,
                  overflowY: 'auto',
                  closeBtnInside: true,
                  preloader: false,
                  midClick: true,
                  removalDelay: 300,
                  blackbg: true,
                  mainClass: 'my-mfp-slide-bottom',
                });
              }
            }
          });
        });
      });

hc8w905p

hc8w905p1#

a)您需要将微调器放在按钮外部
B)最初添加一个类,并在其上应用dispaly:none CSS。
c)现在使用.show().hide()来解决它。
工作样品:

$(document).ready(function() {
  $("#submitButton").click(function(e) {
    e.preventDefault();
    $('.submitSpinner').show();
    setTimeout(
      function() {
         $('.submitSpinner').hide();
      }, 5000);
   
  });
});
.hide {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="d-flex justify-content-center">
  <button class="btn btn-medium btn-fancy btn-primary mb-0 ls-wider" type="submit" id="submitButton">Submit</button>
  <br>
  <span class="submitSpinner spinner-border spinner-border-sm me-1 hide">Send Message...</span>
</div>

相关问题