javascript preventDefault 在提交按钮中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21837155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 21:47:55  来源:igfitidea点击:

preventDefault not work in submit button

javascriptjquery

提问by Val Do

I have htmlform and when I clicked submitbutton page goes to insert.php I don't wont it preventDefoult don't work I have this scripthtml

我有html表单,当我点击submit按钮页面时,会转到 insert.php 我不会它 preventDefoult 不起作用我有这个scripthtml

HTML

HTML

<form action="insert.php" method="post">
<input  type="text" name="username" placeholder=""><br />
<input  type="text" name="name" placeholder=""><br />
<input  type="text" name="lastname" placeholder=""><br />
<input id="mail" name="email" type="text" placeholder="E-mail"><br />
<input id="mail_1" type="text" placeholder="reply E-mail"><br />
<input id="password" name="password" type="password" placeholder=""><br />
<input id="password_1"  type="password" placeholder=""><br /> 
<input id="submit" type="submit" value="registration">
</form>

jquery

查询

$("#submit").click(function(event){
    event.preventDefault();
});

回答by MonkeyZeus

Instead of listening for the button click you need to listen for <form>submit:

您需要监听<form>提交而不是监听按钮点击:

$("form").submit(function(event){
    event.preventDefault();
});


Modern jQuery (1.7 or later):

现代 jQuery(1.7 或更高版本):

$("form").on('submit', function(event){
    event.preventDefault();
});

回答by Paulo Lima

I think you just missed the event which is canceling

我想你只是错过了取消的活动

$( "#target" ).submit(function( event ) {
  event.preventDefault();

});

});

回答by SparK

The problem is the event is bubbling up and the submit event of your form is being called.

问题是事件正在冒泡,并且正在调用表单的提交事件。

Instead of listening the clickevent of your button, you should listen the submitevent of your form:

与其监听click按钮事件,不如监听submit表单事件:

$("#formId").submit(function(event){
    event.preventDefault();
});

And add the idattribute to your form:

并将id属性添加到您的表单中:

<form id="formId" ...

That should stop your form from working.

这应该会阻止您的表单工作。

回答by Dayan

First add an ID to your form, I will use myFormId.

首先在您的表单中添加一个 ID,我将使用myFormId.

<form id="myFormId"  action="insert.php" method="post">
    <input  type="text" name="username" placeholder=""><br />
    <input  type="text" name="name" placeholder=""><br />
    <input  type="text" name="lastname" placeholder=""><br />
    <input id="mail" name="email" type="text" placeholder="E-mail"><br />
    <input id="mail_1" type="text" placeholder="reply E-mail"><br />
    <input id="password" name="password" type="password" placeholder=""><br />
    <input id="password_1"  type="password" placeholder=""><br /> 
    <input id="submit" type="submit" value="registration">
</form>

Then use the form Id:

然后使用表单 ID:

$('#myFormId').on('click', function (event){ 
     event.preventDefault(); 
});

回答by DroidHeaven

To any one lurking around here in future, my problem was that, the form didn't load before the script and hence the event was not bound. So I did the following and my issue was solved:

对于未来潜伏在这里的任何人,我的问题是,表单没有在脚本之前加载,因此事件不受约束。所以我做了以下事情,我的问题解决了:

$(window).load(function () {
    $("#submit").submit(function (e) {
        e.preventDefault();
        ...
        ...
    });
}