简体   繁体   English

jQuery防止默认在WordPress中不起作用

[英]Jquery prevent default doesn't work in wordpress

I have tried by following code to prevent default of form submitting, but it doesn't work. 我尝试通过以下代码来防止默认的表单提交,但它不起作用。 What is problem to that. 这是什么问题。

<div class="section-register">
    <form id="registerform" novalidate="novalidate" method="post" action="http://localhost/yify2/wp-login.php?action=register" name="registerform">
        <input id="wp-submit" class="button button-primary button-large" type="submit" value="Register" name="wp-submit">
    </form>
</div>

My script is : 我的脚本是:

<script type="text/javascript">
    jQuery(document).ready(function() {
    jQuery(".section-register").find("#registerform").submit(function(event){
        if(whatever) { 
            event.preventDefault();  
        }  
      });
   });
</script>

I also tried by following code : 我也尝试通过以下代码:

<script>
jQuery(document).ready(function() {
    jQuery("#registerform").submit( function(e) {

        e.preventDefault();
    });
});
</script>

If your form is loaded via an AJAX request you will need to bind the event after the form is added to the DOM, or bind it on a static node: 如果您的表单是通过AJAX请求加载的,则在将表单添加到DOM之后,您需要绑定该事件,或者在静态节点上绑定该事件:

Delegated event binding : 委托事件绑定

<script>
jQuery(function($) { // Short for jQuery(document).ready(function() {
    // Bind the event to a static node (here I use document). 
    //   The closer to the dynamic node the better.
    // From your code it looks like you might be able to use  
    //   $('.section-register') as the static node.
    $(document).on('submit', '#registerform', function(event) {

        event.preventDefault();
    });
});
</script>

Try this 尝试这个

<script>
jQuery(document).ready(function() {
jQuery("form[name=registerform]").submit( function(e) {

e.preventDefault();
    });
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM