Solved

Trying to close boostrap modal on $.ajax post form.

Posted on 2014-03-03
4
624 Views
Last Modified: 2014-03-08
Hello, I seem to be having a issue with a modal window not fully closing. My order of operation is...

1. Trigger the modal window
2. Do my thing ordering the elements within the modal window
3. Behind the modal my elements re-order themselves.
4. When done and I close the window I'm left with the default smokey bg and the modal window does dissapear but it seems that only half the modal stuff goes away.
5. Everything else works perfectly.

Just the main snippets of code are below...

function persist() {

  var data = $("#sortable").sortable('toArray');
  var datax = "order=" + data;

  $.ajax({
     url: "app/inc/inc_order_categories.cfm",
     dataType: "json",
     data: datax,
     success: function () {

     	$("#myModal2").modal('hide');
     	getData();
     },
     error: function (xhr, ajaxOptions, thrownError) {
     alert(xhr.status);
     alert(thrownError);
     }
  });

}

function getData() {

	$("#getUserData").load("app/inc/inc_get_user_data.cfm").fadeIn("fast");

};

Open in new window


<div id="getUserData"></div>

Open in new window

0
Comment
Question by:brihol44
  • 2
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 300 total points
ID: 39902412
There must be something else going on.  I have a test here http://jsbin.com/hakay/1/ that when you click the blue button the modal will open and close programatically.
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery.min.js"></script>
<link href="http://getbootstrap.com/dist/css/bootstrap.css" rel="stylesheet" type="text/css" />
<script src="http://getbootstrap.com/dist/js/bootstrap.js"></script>
<script>
  $(function() {
 
    $('.btn-lg').click(function(){
   $('#myModal').modal('show')
  
  });
$('#myModal').on('shown.bs.modal', function (e) {
 $('.btn').hide();
  $('#myModal').modal('hide');
})
  });
  </script>
    
  <meta charset="utf-8">
  <title>JS Bin</title>
  <script>
    
    </script>
</head>
<body>
<!-- Button trigger modal -->
<button class="btn btn-primary btn-lg">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <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" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
      stuff
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

</body>
</html>

Open in new window


Can you post your modal code (except the body)

I don't think this has anything to do with it, but be consistent.  Either use double quotes or single quotes. I found when I have done  the same thing my js breaks.
$("#myModal2").modal('hide');

Open in new window

0
 

Author Comment

by:brihol44
ID: 39903841
Thx for the help. Your code is a little different in that I'm making my ajax request. And upon a successful update in the $.ajax part the <div id="getUserData"></div> content refreshes asynchronously from the getData() call .

When I removed that getData() part within the success parameter everything works fine but my content within <div id="getUserData"></div> doesn't reflect the change or get refreshed. Buy maybe it's the " vs ' single tag quotes. I'll try that this evening but I think I did try that as well. thx!
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39904049
I understand. I was just trying to make an easy test.
0
 

Author Closing Comment

by:brihol44
ID: 39915459
I re-worked this but thx!
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question