提交表单时删除模态正文

时间:2017-12-10 14:02:36

标签: javascript jquery html forms bootstrap-modal

HTML:

    <div id="FinalDiv">
      <p>Completed.</p>
    </div>

<div id="modalTeste" class="modal fade" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h2>Title</h2>
      </div>
    <div class="modal-body">Form:</div>
    <form class="radioOpcoes" id="Testemodal">

      <div class="radio">
        <div class="radio">
          <label><input type="radio" name="selection" value="paypal"></label>
        </div>
        <div class="radio">
          <label><input type="radio" name="selection" value="money"></label>
        </div>
        <div class="radio disabled">
          <label><input type="radio" name="selection" value="mastercard"></label>
        </div>
    </form>

    <div class="modal-footer">
      <button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
      <button type="submit" class="btn btn-success" id="removeModal" name="button">Submit</button>

    </div>

  </div>
</div>

JS / Jquery:

  let formEvents = {

    paypal: function() {
      window.open('url');
      $('#modalTeste').hide('fast')
      $('#FinalDiv').show('slow');
    },
    money: function() {
      $('#modalTeste').hide('fast')
      $('#FinalDiv').show('slow');

    },
    mastercard: function() {
      $('#modalTeste').hide('fast')
      $('#FinalDiv').show('slow');
    }
  }

  $('.radioOpcoes').on('submit', function(e) {
    e.preventDefault();
    var value = $(this).find('input:radio:checked').val();

    if (formEvents.hasOwnProperty(value)) {
      formEvents[value].call(this);
    }

  });

  $('#Testemodal').submit(function() {
    $('#modalTeste').modal('toggle');
  });

当我提交表单时,根据用户在表单中选择的选项,隐藏div并显示另一个。然而,即使有这样的工作,它也没有效率,因为它仍然有模态激活:我的目标是,在提交表单时(按提交按钮),每个模态都会消失。我不能使用dismiss-modal因为显示的div(当我使用它时,我的“显示/隐藏”JQuery停止工作)。没有显示错误。

1 个答案:

答案 0 :(得分:1)

您的提交按钮应在表单内,

所以你的HTML会是。

 <div id="FinalDiv">
      <p>Completed.</p>
 </div>

<div id="modalTeste" class="modal fade" role="dialog">
    <form class="radioOpcoes" id="Testemodal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h2>Title</h2>
      </div>
    <div class="modal-body">Form:</div>


      <div class="radio">
        <div class="radio">
          <label><input type="radio" name="selection" value="paypal"></label>
        </div>
        <div class="radio">
          <label><input type="radio" name="selection" value="money"></label>
        </div>
        <div class="radio disabled">
          <label><input type="radio" name="selection" value="mastercard"></label>
        </div>


    <div class="modal-footer">
      <button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
      <button type="submit" class="btn btn-success" id="removeModal" name="button">Submit</button>

    </div>
  </form>
  </div>

如果您的问题得到解决,请与我们联系?

以下是演示https://jsbin.com/mekifeg/edit?html,js,output