0

I have a code to preventDefault after a form submit, and act accordingly to different conditions. Both condition work, everything works, but after the ELSE condition, the page refreshes. Here is the code:

$('.form_invitation').on 'submit', (e) ->
    e.preventDefault()
    $('#sending_invite').show()
    $.post $(this).prop('action'), $(this).serialize(), (invite) ->
        alert(invite.status)
        if invite.status
            $('#sending_invite').hide() 
            $('#invite_sent').show()
            $('#invite_email').val('')
        else
            alert('start')
            $('#sending_invite').hide() 
            $('#invite_fail').show()
            alert('end')

I put these alerts there, and all of them are shown before page refreshes. I've tried returning false instead of preventingDefault, and is also didn't work. Can anyone see what I am missing?

Jesse Mignac
  • 305
  • 2
  • 10
  • What does the network inspector say in terms of requests made after the click? – arghbleargh Aug 30 '14 at 15:28
  • Actualy, it shows that the invite is being submitted twice. They first one has the status false, and the second one is completely empty. – Jesse Mignac Aug 30 '14 at 15:45
  • I just checked again the success scenario, and it is sending two e-mails at time, it means it is being submitted twice also in the success scenario! – Jesse Mignac Aug 30 '14 at 15:49
  • OK, now how about if you don't do anything in the submit handler besides `e.preventDefault()`? – arghbleargh Aug 30 '14 at 20:54
  • If I only have the e.preventDefault(), it refreshes the page with nothing built in the network inspector – Jesse Mignac Aug 31 '14 at 01:34
  • 1
    This might be relevant: http://stackoverflow.com/questions/9347282/using-jquery-preventing-form-from-submitting. Maybe you can give us more code, such as the HTML? Even better if you can reproduce it in a JSFiddle (I couldn't). It does seem like a strange problem... – arghbleargh Aug 31 '14 at 04:14
  • Found out the error. I had another form with the same id of this one, and I missed that. That's why the form was being submitted twice. Thanks, arghbleargh! – Jesse Mignac Aug 31 '14 at 21:42

1 Answers1

0

Just found out what was happening. When rails generated the form, it made it with the same name of another form I had for a similar purpose, and I've already had a js function to deal with the other one. All I had to do was change de id of this one, and everything works now.

Jesse Mignac
  • 305
  • 2
  • 10