Добавить событие form.submit, чтобы убедиться, что сообщение происходит до отправки формы? - PullRequest
4 голосов
/ 10 сентября 2010

У меня есть форма.Я хочу добавить обработчик событий, используя jquery.

$("form").submit(function blah() {
   $.post('ping-me-please.php', params, function (response) {
       // only submit the form once this goes through!
   });

}

Как я могу это сделать?

Ответы [ 2 ]

5 голосов
/ 10 сентября 2010

Как это:

$("form").submit(function (e) {
    var form = $(this);

    if (!form.data('pinged')) {
        e.preventDefault(); // Cancel the submit
        $.post('ping-me-please.php', params, function (response) {
            // only submit the form once this goes through!

            // First we set the data, then we trigger the submit again.
            form.data('pinged', true).submit();
        });
    }
}
0 голосов
/ 10 сентября 2010
var postWasSent = false;
$("form").submit(function blah() {           
   if (!postWasSent)
   {
      $.post('ping-me-please.php', params, function (response) {
         postWasSent=True;
         $("form").submit();
      });
      return false;
   }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...