Skip to content

Instantly share code, notes, and snippets.

@jbatista19
Forked from havvg/ajax-form.js
Last active December 13, 2015 20:48
Show Gist options
  • Save jbatista19/4972740 to your computer and use it in GitHub Desktop.
Save jbatista19/4972740 to your computer and use it in GitHub Desktop.
Bootstrap Modal ajaxified live() replaced with on()
jQuery(function($) {
$('body').on('submit','form[data-async]', function(event) {
alert('submit Event');
var $form = $(this);
var $target = $($form.attr('data-target'));
$.ajax({
type: $form.attr('method'),
url: $form.attr('action'),
data: $form.serialize(),
success: function(data, status) {
$target.html(data);
}
});
event.preventDefault();
});
});
<!-- Bootstrap trigger to open modal -->
<a data-toggle="modal" href="#rating-modal">Write a Review</a>
<div class="hide fade modal" id="rating-modal">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h2>Your Review</h2>
</div>
<div class="modal-body">
<!-- The async form to send and replace the modals content with its response -->
<form class="form-horizontal well" data-async data-target="#rating-modal" action="/some-endpoint" method="POST">
<fieldset>
<!-- form content -->
</fieldset>
</form>
</div>
<div class="modal-footer">
<a href="#" class="btn" data-dismiss="modal">Cancel</a>
</div>
</div>
@jbatista19
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment