表单onsubmit preventDefault()不起作用

时间:2020-07-09 06:26:20

标签: javascript php jquery

在提交以下表单时,它总是使用url中的参数重新加载。不知道为什么会这样:

$html .= 
      '<form method="post" id="mailchimp">
           <input type="text" name="fname" placeholder="First name" />
           <input type="text" name="lname" placeholder="Last name" />
           <input type="email" name="email" placeholder="Email *" required />
           <button type="submit" id="mailchimp_submit">Subscribe</button>
       </form>';

jquery在下面

jQuery(document).ready(function() {
    jQuery('#mailchimp').on('submit', function(e){
        console.log('submitting');
        e.preventDefault();
    });
});

它既没有控制台日志也没有阻止加载。它只是一直加载?我有什么编码错误吗?

1 个答案:

答案 0 :(得分:0)

我认为您尚未添加JQuery路径。 我测试了这段代码,效果很好