У меня есть форма.Я хочу добавить обработчик событий, используя jquery.
$("form").submit(function blah() { $.post('ping-me-please.php', params, function (response) { // only submit the form once this goes through! }); }
Как я могу это сделать?
Как это:
$("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(); }); } }
var postWasSent = false; $("form").submit(function blah() { if (!postWasSent) { $.post('ping-me-please.php', params, function (response) { postWasSent=True; $("form").submit(); }); return false; } }