On click functionality is working fine but when I am using change function then it not hitting submit function
Example 1:
$('button').click(function(e) {
$('form').submit(function(event) {
event.preventDefault(); //prevent default action
var post_url = $(this).attr("action"); //get form action url
var request_method = $(this).attr("method"); //get form GET/POST method
var form_data = new FormData(this); //Encode form elements for submission
$.ajax({
url : post_url,
type: request_method,
data : form_data,
contentType: false,
processData:false,
}).done(function(response) { //
//pass
}).fail(function(response) {
//fail
});
});
});
Example 2:
$('input').change(function(event) {
$(this).parents('form').submit(function(event) {
event.preventDefault(); //prevent default action
var post_url = $(this).attr("action"); //get form action url
var request_method = $(this).attr("method"); //get form GET/POST method
var form_data = new FormData(this); //Encode form elements for submission
$.ajax({
url : post_url,
type: request_method,
data : form_data,
contentType: false,
processData:false,
}).done(function(response) {
//pass
}).fail(function(response) {
//fail
});
});
});
Can someone please help me Why Example 2 is not working as I wanted. If you see both code is same but only change in the event name.
am I making mistake or its Jquery default behavior?