I have a simple HTML form such as follows:
I want to capture and process the submit event of the form in Javascript by using the following code:
var jqxhr = $.post("controller/process.php", "", function(html) { $("#return_msg").html(html); }) .fail(function(x, e) { error_alert(x,e); }) .complete (function(){ console.log("sub_cat finished"); });
The default submit event could cause the page to refresh. As a result, html returned from process.php will be wiped before user can see it on the screen. Add the following event handling to prevent this from happening.
$('#content').on('submit', '#get_form', function() { return false; });
Here’s another way:
$("#get_form").submit(function(e) { e.preventDefault(); });