I'm trying to stop this JQuery POST call from submitting twice. Tried a few different variants of the unbind but don't understand it entirely.
I have seen similar questions, but they only state to use unbind, and not why or how for different scenarios.
$('.submit').click(function(){
if (error != 1 && submitted != 1 && emailError != 1) {
$(this).css('cursor', 'default');
$(this).fadeTo(0, 0.50);
submitted = 1;
if($(this).parents('form').hasClass('ajaxSend')) {
var url = $(this).parents('form').attr('action');
var fields = {};
$(this).parents('form').find(':input').each(function(){
var name = $(this).attr('name');
var value = $(this).val();
fields[name] = value;
});
$.post(url, fields).done(function(data){
$(".statusMessage").html(data);
$('.modal, .modalClose').css('margin-top' , 120 + $(document).scrollTop() + 'px');
$('.modal, .modalClose').hide();
$('.statusMessage, .modalClose').fadeTo('fast', 1);
$('.modalCover').fadeTo('fast', 0.80);
});
$('.submit').unbind('click').submit();
return false;
}
}
});
You should be doing this on the Form submit not the button click:
$("#my-form").on('submit', function(e){
//do ajax stuff
e.preventDefault(); //Stop form submitting via browser default functionality
});
what about disable submit button!
$('#form').submit(function(){
var button = $('#submit');
$.ajax({
url: '',
type: 'post',
dataType: 'html',
success: function(data){
// if OK
button.prop('disabled',false);
},
beforeSend: function(){
button.prop('disabled',true);
},
error: function() {}
});
return false;
});