skip to Main Content

I am using bootstrap 3.3.6 and i have the modal dialog code opens like this

<div id="myModalPositions" class="modal fade">
<div class="modal-dialog">
    <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">ACT</h4>
            </div>
            <div class="modal-body">
                <p>Loading...</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
    </div>
</div>

<script type="text/javascript">
$('.openStandings').on('click', function(e){
  e.preventDefault();
  $('#myModalPositions').modal('show').find('.modal-body').load($(this).attr('data-href'));
});
</script>   

inside the myModalPositions, the data-href loads a file from the server which i am doing

so i added a code inside the data-href = url where i have the form and submit and added the ajax code inside that data-href file,

every time i am trying to submit, it is closing my modal dialog

This is the ode inside the data-href url

<form name="form1" id="form1">
<input type="text" name="name" id="name">
<button type="submit" class="btn btn-primary updateData" data-dismiss="modal">Proceed</button>  
</form>
<script type="text/javascript" src="../jquery/jquery-2.1.0.js"></script>
<script type="text/javascript">
    $("#form1").submit(function (event) {
        event.preventDefault();
        var formData = $(this).serialize();
        $.ajax({
          type: "POST",
          url: "proceed.cfm",
          data: formData
        }).done(function (data) {
          console.log(data);
        });
    });
</script>   

what i am doing wrong here

2

Answers


  1. Your button in the form has attribute data-dismiss="modal" so it closes the modal.

    <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
    
    
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    
    <!-- Latest compiled and minified JavaScript -->
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
    
    
    
    <div id="myModalPositions" class="modal fade">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title">ACT</h4>
          </div>
          <div class="modal-body">
    
            <form name="form1" id="form1">
              <input type="text" name="name" id="name">
              <button type="submit" class="btn btn-primary updateData" >Proceed</button>
            </form>
    
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </div>
      </div>
    </div>
    
    
    
    <button class="openStandings">openStandings</button>
    
    <script type="text/javascript">
      $('.openStandings').on('click', function(e) {
        e.preventDefault();
        $('#myModalPositions').modal('show').find('.modal-body').load($(this).attr('data-href'));
      });
    
    
      $("#form1").submit(function(event) {
        event.preventDefault();
        console.log("sbumit");
        var formData = $(this).serialize();
        $.ajax({
          type: "POST",
          url: "proceed.cfm",
          data: formData
        }).done(function(data) {
          console.log(data);
        });
      });
    </script>
    Login or Signup to reply.
  2. use data-backdrop="static"

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search