Bootstrap modal opening on second click / modal inside modal

时光毁灭记忆、已成空白 提交于 2019-12-23 03:24:14

问题


I have two modals, one inside another. The first modal contains a form and on submit I want another modal with a message in it to open. I made this with Bootstrap. First modal opens when this button is clicked :

<button class="contact-button" data-toggle="modal" data-target="#contact-modal">Contactează-ne</button>

And this is the first modal:

<div id="contact-modal" class="modal fade" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Contactaţi-ne</h4>
      </div>

      <div class="modal-body">

        <form id="contact-form">
          <div class="col-md-12">
            <h4>Numele Dvs *</h4>
            <input type="text" name="name" value="" placeholder="">
          </div>
          <div class="col-md-6">
            <h4>Email *</h4>
            <input type="text" name="email" value="" placeholder="johndoe@yahoo.com">
          </div>
          <div class="col-md-6">
            <h4>Telefon *</h4>
            <input type="text" name="tel" value="" placeholder="+40******">
          </div>
          <div class="col-md-12">
            <h4>Mesaj *</h4>
            <textarea></textarea>
          </div>
          <div class="col-md-12"><h6>*câmpurile sunt obligatorii</h6></div>
          <div class="col-md-12 call">
           <button>Trimite Mesaj</button>
          </div> 
        </form>
      </div> 
    </div>
  </div>
</div>

On submit, the second modal opens and I want the first one to close when this happens. I achieved this with this script:

$('form#contact-form').submit(function(e){
    e.preventDefault();
    $('#call-answer').modal();
    $('#contact-modal').hide(); // first modal closes
})

#call-answer is the second modal, it's also made with Bootstrap, I only open it manually. So, when the page loads everything works fine, I click the button, the contact form opens in a modal, on submit it closes and the second modal opens. Now the problem is that if I click again on the button that opens the contact form modal, it doesen't open until I click two times. I know the problem has to do with my script, because if I remove the line

$('#contact-modal').hide();

everything works just fine. Has anyone a better idea on how can I close the first modal when the second opens? or how can I solve this? Thanks


回答1:


Issue is with this line $('#contact-modal').hide();

Don't use hide when working with plugins

Use this line$('#contact-modal').modal('hide');

Demo

Reference



来源:https://stackoverflow.com/questions/32392691/bootstrap-modal-opening-on-second-click-modal-inside-modal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!