阻止使用jQuery提交HTML表单

阻止使用jQuery提交HTML表单

问题描述:

我有一个使用mailchimp的订阅表单。阻止使用jQuery提交HTML表单

我已经写了一个JS验证方法来阻止hotmail/yahoo/gmail帐户注册只有我不能得到它的工作?不管试了一下香港专业教育学院的形式作用踢和JS被忽略......

表单标签

<form id="mc-embedded-subscribe-form" class="validate" action="action here" method="post" name="mc-embedded-subscribe-form" novalidate="" target="_blank"> 

JS验证

$('#mc-embedded-subscribe-form').on('submit', function (e) { 

var inputString = $('#mce-EMAIL').val(); 
var gmail = "gmail"; 
var yahoo = "yahoo"; 
var hotmail = "hotmail"; 

if (inputString.indexOf(gmail) > -1) { 
    alert('Sorry, Gmail accounts are unable to subscribe'); 
    return false; 

} else if (inputString.indexOf(yahoo) > -1) { 

    alert('Sorry, Yahoo accounts are unable to subscribe');return false; 
} else if (inputString.indexOf(hotmail) > -1) { 

    alert('Sorry, Hotmail accounts are unable to subscribe'); return false; 
} else { 
    return true; 
} 

}); 

你应该能够防止从提交通过禁用事件的默认操作提交的表单:

$('#mc-embedded-subscribe-form').on('submit', function (e) { 

    e.preventDefault(); 
    . . . 
}); 

您可以防止它立即发射,然后手动触发提交,也可以在出现错误时致电preventDefault()