2

I have this function. I don't understand why the form is submitted twice. Infact the request I see at the server hapijs is double.

$(function(){

  $('form').on('submit', function(event){

    event.preventDefault();
    var $form = $(this);

    $.post('/send-message', {
      data: $form.serialize()
    }, function(data) {

    })
    .fail(function(data){

    });
  });
});

EDIT:

I have realized that placing the script at the top is executed just once. Why?

Mazzy
  • 13,354
  • 43
  • 126
  • 207
  • 1
    why don't you test using `action="#"` as you really don't care about the form action url... ? and btw, don't use `event`, use `evt` or `e`... `event` is not a javascript keyword, but in IE [it's a global variable](http://stackoverflow.com/a/1510106/28004)... – balexandre Sep 20 '14 at 16:32
  • you are selecting all forms using this selector, is it possible that you have several forms on the page? – Banana Sep 20 '14 at 16:33
  • no I have just once form in the page. – Mazzy Sep 20 '14 at 16:42
  • Ok solved. I have placed the script at the top instead at the bottom of the page. – Mazzy Sep 20 '14 at 17:35
  • Ok now the question is: Why a script placed at the bottom of the paged is executed twice? – Mazzy Sep 20 '14 at 17:44
  • Ok fixed. for a weird reason hapijs template server double the scripts at the bottom. now it works like a charm – Mazzy Sep 20 '14 at 18:01
  • @Mazzy - You should answer your own question since it seems to found a bug and a workaround. – jww Sep 21 '14 at 16:15
  • @jww simply adding an answer is useless unless the question is updated accordingly so that somebody else finds it. Currently the question isn't even tagged with the plugin having the bug... – T J Sep 21 '14 at 16:22
  • @TJ - if you have the subject matter expertise, then you might consider doing it. I don't have the expertise (I experienced the question because it showed up in a review queue). – jww Sep 21 '14 at 16:24
  • I don't have either.. i just googled hapi.js.. o.0 – T J Sep 21 '14 at 16:25
  • You might try to unbind the submit handler right before binding it to your element as such: $('form').unbind('submit', handler ); This should ensure that any previously-attached submit handler is removed and leaves the one intended. – ImaJedi4ever Oct 10 '14 at 19:37

0 Answers0