title: Close jQuery modal popup using link in AJAX content
body: <p>I have a jQuery modal window in which I load a form with a submit button. I would like to add a "cancel" link which would clear the form and close the popup.</p>
  
  <p>I am using the code that comes with the new ASP.NET MVC4 default template. I am looking for a script to make this work. </p>
  
  <p>This is the current code used to store and display dialogs:</p>
  
  <pre><code>$(function () {
      // Cache for dialogs
      var dialogs = {};
      var loadAndShowDialog = function (id, link, url) {
          var separator = url.indexOf('?') &gt;= 0 ? '&amp;' : '?';
  
          // Save an empty jQuery in our cache for now.
          dialogs[id] = $();
  
          // Load the dialog with the content=1 QueryString in order to get a PartialView
          $.get(url + separator + 'content=1')
              .done(function (content) {
                  dialogs[id] = $('&lt;div class="modal-popup"&gt;' + content + '&lt;/div&gt;')
                      .hide() // Hide the dialog for now so we prevent flicker
                      .appendTo(document.body)
                      .filter('div') // Filter for the div tag only, script tags could surface
                      .dialog({ // Create the jQuery UI dialog
                          title: link.data('dialog-title') || 'Inloggen',
                          modal: true,
                          resizable: false,
                          draggable: false,
                          width: link.data('dialog-width') || 370,
                          beforeClose: function () { resetForm($(this).find('form')); }
                      })
                      .find('form') // Attach logic on forms
                          .submit(formSubmitHandler)
                          .find('a.close-dialog').click(function (id) {
                              //dialog close logic here
                          }).end();
  
              });
      };
  
      var links = ['.loginLink'];
  
      $.each(links, function (i, id) {
          $(id).click(function (e) {
              var link = $(this),
                  url = link.attr('href');
  
              if (!dialogs[id]) {
                  loadAndShowDialog(id, link, url);
              } else {
                  dialogs[id].dialog('open');
              }
  
              // Prevent the normal behavior since we use a dialog
              e.preventDefault();
          });
      });
  </code></pre>