【问题标题】:Prevent HTML form from submitting with jQuery防止 HTML 表单使用 jQuery 提交
【发布时间】:2015-08-20 21:02:11
【问题描述】:

我有一个使用 mailchimp 的订阅表格。

我已经编写了一个 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;
}

});

【问题讨论】:

    标签: javascript jquery forms mailchimp


    【解决方案1】:

    您应该能够通过禁用事件的默认操作来阻止表单提交:

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

    您可以阻止它立即触发,然后稍后手动触发提交,或者您可以仅在出现错误时调用preventDefault()

    【讨论】:

      猜你喜欢
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多