-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a script to automatically refresh a form (#24)
- Loading branch information
1 parent
ea29d1a
commit e02a271
Showing
3 changed files
with
66 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// For every change on inputs and select with attr data-refreshOnChange | ||
$(document).on( | ||
'change', | ||
'select[data-refreshOnChange="true"], ' + | ||
'input:checkbox[data-refreshOnChange="true"], ' + | ||
'input:radio[data-refreshOnChange="true"]', | ||
function(event) { | ||
var form = $(this).parents('form'); | ||
var formdata = (window.FormData) ? new FormData(form[0]) : null; | ||
var data = (formdata !== null) ? formdata : form.serialize(); | ||
var ignoredChanges = {}; | ||
$(form).find('[data-ignoreonchange]').each(function (idx, elem) { | ||
var $elem = $(elem); | ||
ignoredChanges[$elem.attr('data-ignoreonchange')] = $elem; | ||
}); | ||
$.ajax({ | ||
url : form.attr('action') + (form.attr('action').split('?')[1] ? '&' : '?') + 'novalidate', | ||
data : data, | ||
type : $(form).attr('method'), | ||
contentType : false, | ||
dataType : 'json', | ||
processData : false, | ||
}).done(function(response){ | ||
if (typeof response === 'object' && response.hasOwnProperty("html")) { | ||
form.html(response.html); | ||
} else { | ||
form.html(response); | ||
} | ||
$.each(ignoredChanges, function(index, elem){ | ||
$('[data-ignoreonchange="' + index + '"]').replaceWith(elem); | ||
}); | ||
}).fail(function(response) { | ||
console.log(response); | ||
}); | ||
} | ||
); |