My Ajax request works correctly when I use change and the input is checkbox, but my Ajax request does not work when use input type submit !!
I want my type in the input submit and when I do this the Ajax request will not work and the page will reload.
$(document).on('change','.filter-form',function(event){
event.preventDefault();
$.ajax({
type:'GET',
url:'filter/',
data : $(this).serialize(),
dataType: 'json',
success: function (data) {
$('#product-main').html(data['form']);
},
error: function (data) {
alert("error" + data);
}
});
});
my form :
<form action="" class="filter-form">
<input type="submit" name="price" value="new">
<input type="submit" name="discount" value="discount">
<input type="submit" name="old" value="old">
</form>
2
Answers
First added a
clicked
property to the identify which submit is clicked. Then used the clicked property to get thevalue
&name
of the submit to pass in form submit event handler.I don’t think submit buttons trigger the change event, so you’ll have to listen for something else, also
.serialize()
do not give you the name/value pair of submit buttons.Use the click event on the buttons and use the element properties to get the data to post.