event.preventDefault();仅在某些时候使用提交表单
event.preventDefault(); only works some of the time with submit form
我正在使用Mailchimp API提交表单。目标是防止 Mailchimp 提供的默认回调。大多数时候,event.preventDefault()
的行为是应有的。然后随机它将不起作用:
$(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 to the registration server. Please try again later."); },
success : function(data) {
if (data.result != "success") {
// Something went wrong, do something to notify the user. maybe alert(data.msg);
var message = data.msg
var messageSh = data.msg.substring(4);
if (data.msg == '0 - Please enter a value' || data.msg == '0 - An email address must contain a single @') {
$('#notification_container').html('<span class="alert">'+messageSh+'</span>');
} else {
$('#notification_container').html('<span class="alert">'+message+'</span>');
}
} else {
// It 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);
}
}
});
}
尝试
- 起飞准备功能。
- 如果事件打开则删除
法典:
var $form = $('#mc-embedded-subscribe-form');
$('#mc-embedded-subscribe').on('click', function(event) {
event.preventDefault();
register($form);
});
相关文章:
- 如何使用提交按钮搜索表中的记录
- 使用提交按钮隐藏未选中的单选按钮
- 使用提交按钮参数自动提交表单
- 使用提交按钮提交表单,而不是页面上的其他按钮
- 在不使用提交按钮重新加载的情况下将表单提交到PHP页面
- 我可以使用提交按钮和JS提交表单吗?
- 使用提交按钮打开多个鲍泽选项卡
- PHP / 如何使用提交按钮发布下拉菜单选项
- 使用提交按钮重构 AJAX 表单的建议
- 如何使用提交按钮添加多个下拉表单 合并值并转到网址
- 使用提交验证日期输入字段
- event.preventDefault();仅在某些时候使用提交表单
- 当I'I’我没有使用提交按钮
- 服务器脚本可以检测表单是使用提交按钮还是使用javascript:submit()发布的
- 通过单击链接提交表单,而不使用提交按钮
- 使用Html.BeginForm asp.net MVC阻止在使用@提交时重新加载页面
- 使用提交按钮功能调试javascript中的问题
- 如何使用提交按钮(onclick)在javaScript(服务器端)上创建消息框
- 为什么不't当我尝试在不使用提交按钮的情况下提交时,我的表单提交
- 我可以使用提交按钮来激活AJAX功能吗