使用JQuery - 阻止表单提交 [英] Using JQuery - preventing form from submitting

查看:86
本文介绍了使用JQuery - 阻止表单提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过了一切 - 看到我在下面尝试了3种不同的选项,但它都不起作用:

如何防止使用jquery提交表单? / p>

  $(document).ready(function(){

//选项A
$(#form)。submit(function(e){
e.preventDefault();
});

//选项B
$( #form)。submit(function(e){
stopEvent(e);
});

//选项C
$(#form ).submit(function(){
return false;
});
});

什么可能是错误的?

更新 - 这里是我的HTML:

 < form id =formclass =formaction =page2.php方法= POST > 
<! - 表单中的标签 - >
< p class =class2>
< input type =submitvalue =好的! />
< / p>
< / form>

这里有什么不对吗?

解决方案

有两点值得注意:



  • 有可能您的表单名称不是形式。相反,通过删除#来引用
    标签。
  • e.preventDefault 语法,例如

      //选项A 
    $(form)。submit(function(e){
    e.preventDefault();
    });




选项C也可以。我不熟悉选项B



一个完整的例子:

  < HTML> 
< head>
< script type =text / javascriptsrc =http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js>< / script>

< script type ='text / javascript'>
$(document).ready(function(){
// option A
$(form)。submit(function(e){
alert('submit intercepted ');
e.preventDefault(e);
});
});
< / script>
< / head>

< body>
< form action =http://google.commethod =GET>
搜寻< input type ='text'name ='q'/>
< input type ='submit'/>
< / form>
< / body>
< / html>


How do I prevent a form from submitting using jquery?

I tried everything - see 3 different options I tried below, but it all won't work:

    $(document).ready(function() { 

            //option A
            $("#form").submit(function(e){
                e.preventDefault();
            });

            //option B
            $("#form").submit(function(e){
                stopEvent(e);
            });

            //option C
            $("#form").submit(function(){
                return false;
            });
    });

What could be wrong?

Update - here is my html:

    <form id="form" class="form" action="page2.php" method="post"> 
       <!-- tags in the form -->
       <p class="class2">
           <input type="submit" value="Okay!" /> 
       </p>
    </form>

Is there anything wrong here?

解决方案

Two things stand out:

  • It possible that your form name is not form. Rather refer to the tag by dropping the #.
  • Also the e.preventDefault is the correct JQuery syntax, e.g.

        //option A
        $("form").submit(function(e){
            e.preventDefault();
        });
    

Option C should also work. I am not familiar with option B

A complete example:

<html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

        <script type='text/javascript'>
         $(document).ready(function() {
            //option A
            $("form").submit(function(e){
                alert('submit intercepted');
                e.preventDefault(e);
            });
        });
        </script>
    </head>

    <body>
        <form action="http://google.com" method="GET">
          Search <input type='text' name='q' />
          <input type='submit'/>
        </form>
    </body>
</html>

这篇关于使用JQuery - 阻止表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆