javascript - event.preventDefault(); only works some of the time with submit form -
i using mailchimp api submit form. goal prevent default callback provided mailchimp. majority of time event.preventdefault() behaving should. randomly not work:
$(function () { var $form = $('#mc-embedded-subscribe-form'); $('#mc-embedded-subscribe').on('click', function(event) { if(event) event.preventdefault(); register($form); }); }); function register($form) { $.ajax({ type: $form.attr('method'), url: $form.attr('action'), data: $form.serialize(), cache : false, datatype : 'json', contenttype: "application/json; charset=utf-8", error : function(err) { alert("could not connect registration server. please try again later."); }, success : function(data) { if (data.result != "success") { // went wrong, notify user. maybe alert(data.msg); var message = data.msg var messagesh = data.msg.substring(4); if (data.msg == '0 - please enter value' || data.msg == '0 - email address must contain single @') { $('#notification_container').html('<span class="alert">'+messagesh+'</span>'); } else { $('#notification_container').html('<span class="alert">'+message+'</span>'); } } else { // worked, carry on... var message = data.msg; $('.popup-promo-container').addclass('thanks'); $('.checkboxes, #mc_embed_signup_scroll').addclass('hidden'); $('.complete-promo').html(message).removeclass('hidden'); settimeout(function() { document.queryselector('.popup-promo').style.display = "none"; },20000); } } }); }
try
- take off ready function.
- remove if on event
code:
var $form = $('#mc-embedded-subscribe-form'); $('#mc-embedded-subscribe').on('click', function(event) { event.preventdefault(); register($form); });
Comments
Post a Comment