使用 JQuery - 防止表单提交 [英] Using JQuery - preventing form from submitting
本文介绍了使用 JQuery - 防止表单提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何防止使用 jquery 提交表单?
How do I prevent a form from submitting using jquery?
我尝试了所有方法 - 请参阅下面我尝试过的 3 个不同选项,但都不起作用:
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;
});
});
可能有什么问题?
更新 - 这是我的 html:
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>
这里有什么问题吗?
推荐答案
有两点很突出:
Two things stand out:
- 您的表单名称可能不是
form
.而是参考通过删除 # 标记. 还有
e.preventDefault
是正确的 JQuery 语法,例如
- 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();
});
选项 C 也应该有效.我不熟悉选项 B
Option C should also work. I am not familiar with option B
一个完整的例子:
<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屋!
查看全文