Here's my solution. It simply makes an additional request to the form to grab the matrix fields and reinitializes them. I had to trim this up so it may or may not work in it's entirety but you get the idea.
$(function () {
// ajax form post
$("button[type='submit']").bind('click', function (e) {
// select your form
var $form = $('#yourform');
// submit the form
$.ajax({
type: "POST",
dataType: "json",
url: $form.attr('action'),
data: $form.serializeArray();,
success: function (data) {
if (data.success)
{
reloadMatrices(); // here's the magic
if ($form.find('[name="action"]').val() == "submit") // your form will have multiple <button name="action"> tags with different values such as "submit" or "save"
{
alert("Thank you for your submission! We will be contacting you shortly.");
window.location = "//" + window.location.hostname + "/confirmation"; // redirect to confirmation page
}
}
}
});
});
});
/**
* function reloadMatrices()
*
* Requests the same form and replaces all instances of a Matrix fieldtype with the new form from the request and reinitializes them.
*/
function reloadMatrices() {
$.ajax({
type: "GET",
url: window.location,
success: function (data) {
var $data = $(data);
// you might be able to use .load() here instead for more concise code
$('.matrix-ee2').each(function () {
var $this = $(this);
$this.replaceWith($data.find("#" + $this.attr('id')));
// reinitialize all of the matrices so they work as intended
// IMPORTANT: requires an md5 jQuery plugin such as https://github.com/placemarker/jQuery-MD5
window['initMatrix_' + $.md5($this.attr('id'))]();
});
}
});
}