【问题标题】:Bootstrap modal fires twice when close and reopen关闭并重新打开时,引导模式会触发两次
【发布时间】:2019-06-29 10:10:37
【问题描述】:

我有一个通过单击按钮调用的登录表单模式。并且模态是从带有ajax加载的外部文件调用的。但是,如果我关闭并重新打开模式。模态将启动两次并出现双模态背景类,我无法使用模态的功能。此外,我发现控制台消息出现: [DOM] Found 2 elements with non-unique id #loginForm: 。

当模态隐藏时,我尝试删除模态类。但是,它不起作用。

//jquery

$('a.login').click(function(event) {
  var url = "userlogin.php";
  $('.login-container').load(url,function(result){
      $('#modalLoginForm').modal({show:true});

        });

});

$("#modalLoginForm").on("hidden.bs.modal", function () {
  resetMyForm();
  $("#modalLoginForm").remove();
});

$('.loginbutton').click(function(event) {
// prevent the default submit
alert("haha");
event.preventDefault();
var form = $('#loginForm');
//alert(form);
$.ajax({
    url: "authentication.php", 
    type: "POST",
    // use the forms data
    data: form.serialize(),
    beforeSend: function() {    
        console.log( "Processing..." );
    },
    success: function( response ){
        // do sth with the response

        if(response == "OK") {
           // credentials verified
           // redirect

         location.reload();
        }else{
           // credentials incorrect
           // append errormessage to modal
          form.closest('.modal-body').append('<div class="error text- 
          danger">*'+response+'</div>');

          }
          },
    error: function( response ) {
       console.log(response);
    }

    });
    return false;
    });

//index.php

 <a href="" class="btn btn-secondary btn-rounded login" data- 
 toggle="modal">Login</a>
<div class="login-container"></div>

//userlogin.php

    <div class="modal fade" id="modalLoginForm" tabindex="-1" 
    role="dialog" aria-labelledby="myModalLabel"
    aria-hidden="true">
    <div class="modal-dialog" role="document">
    <div class="modal-content bg-light">
    <div class="modal-header bg-dark">
    <h4 class="col-sm-12 text-center text-white comp ">Sign in</h4>
    </div>
    <div class="modal-body mx-3">
  <div class="container">
  <div class="col-sm-12">
  <form id="loginForm"  method="post">
    <div class="md-form mb-5">
      <i class="fas fa-envelope prefix grey-text"></i>
      <label data-error="wrong" data-success="right" 
     for="defaultForm-email">Username:</label>
     <input type="text" name="username" class="form-control validate">
    </div>

    <div class="md-form mb-4">
      <i class="fas fa-lock prefix grey-text"></i>
       <label data-error="wrong" data-success="right" 
     for="defaultForm-pass">Password:</label>
     <input type="password" name="password" class="form-control validate">

      </div>
    </div>
    <form>
 </div>
  </div>
  <div class="modal-footer d-flex justify-content-center bg-primary">
    <button type="submit" class="btn btn-default text-white comp 
   loginbutton">Login</button>
   </div>
   </div>
   </div>
   </div>

   <script src="js/modal.js"></script>

当我关闭并重新打开时,我只想启动一个模式。感谢您的帮助。

【问题讨论】:

    标签: javascript jquery bootstrap-modal


    【解决方案1】:

    尝试在 JS 中这样做,请使用“本机”选项来显示和隐藏模态,也就是模态。('toggle')

    我的建议是,将除 modal-body 内的内容之外的所有 modal 移至 index.php,并将表单 (#loginForm) 留在 userlogin.php 中,然后为此更改您的 javascript:

    $('a.login').click(function(event) {
      var url = "userlogin.php";
      $('#modalLoginForm').find('.modal-body').load(url)
      $('#modalLoginForm').modal('toggle');
    });
    
    $("#modalLoginForm").on("hidden.bs.modal", function () {
      resetMyForm();
    });
    

    有时,当模态显示重复或直接不工作时,这是因为 jQuery 或 Boostrap 导入的顺序错误或重复,但在您的情况下,我认为它只是错误的 javascript。

    【讨论】:

    • 谢谢。但是,我使用上面的代码并且无法启动模式。我添加了 alert(url) 函数以确保触发点击事件。但是无法启动模态。
    • 感谢您的帮助。您的建议帮助我解决模态问题。但是,它会触发我的登录按钮单击作为乘数。第一次打开模态并单击登录警报2次,然后是4次,8次。谢谢你帮助我。上列中更新的代码
    • 我通过 $('.loginbutton').unbind().click(function(event) 解决问题。
    • @shelvey33 很高兴我能帮上忙!请投票并标记为已回答:)
    【解决方案2】:

    你调用了 modal 两次:data-togglemodal 函数

    在您的index.php 中尝试此代码:

    <a href="" class="btn btn-secondary btn-rounded login">Login</a>
    <div class="login-container"></div>
    

    如果模态框在调用load之前已经加载到您的页面,请添加一些验证

    【讨论】:

    • 谢谢。我尝试了上面的代码,我发现背景出现 1 秒然后立即消失。
    猜你喜欢
    • 2014-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多