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
preventDefault not work in submit button
提问by Val Do
I have html
form and when I clicked submit
button page goes to insert.php
I don't wont it
preventDefoult don't work
I have this script
html
我有html
表单,当我点击submit
按钮页面时,会转到 insert.php 我不会它 preventDefoult 不起作用我有这个script
html
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 click
event of your button, you should listen the submit
event of your form:
与其监听click
按钮事件,不如监听submit
表单事件:
$("#formId").submit(function(event){
event.preventDefault();
});
And add the id
attribute 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();
...
...
});
}