防止表单提交上的默认 jQuery
这有什么问题吗?
HTML:
<form action="<URL>http://localhost:8888/bevbros/index.php/test"
method="post" accept-charset="utf-8" id="cpa-form" class="forms">
<input type="text" name="zip" id="Zip" class="required valid">
<input type="submit" name="Next" value="Submit" class="forms" id="1">
</form>
jQuery:
$("#cpa-form").submit(function(e){
e.preventDefault();
});
What's wrong with this?
HTML:
<form action="<URL>http://localhost:8888/bevbros/index.php/test"
method="post" accept-charset="utf-8" id="cpa-form" class="forms">
<input type="text" name="zip" id="Zip" class="required valid">
<input type="submit" name="Next" value="Submit" class="forms" id="1">
</form>
jQuery:
$("#cpa-form").submit(function(e){
e.preventDefault();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
试试这个:
Try this:
使用新的“on”事件语法。
引用:https://api.jquery.com/on/
Use the new "on" event syntax.
Cite: https://api.jquery.com/on/
这是一个古老的问题,但这里接受的答案并没有真正触及问题的根源。
您可以通过两种方式解决这个问题。首先使用 jQuery:
或者不使用 jQuery:
您不需要使用
return false
来解决这个问题。This is an ancient question, but the accepted answer here doesn't really get to the root of the problem.
You can solve this two ways. First with jQuery:
Or without jQuery:
You don't need to use
return false
to solve this problem.我相信上面的答案都是正确的,但这并没有指出为什么
submit
方法不起作用。好吧,如果 jQuery 无法获取
form
元素,则submit
方法将不起作用,并且 jQuery 不会给出任何相关错误。如果您的脚本放置在文档的头部,请确保代码在DOM
准备就绪之后运行。因此,$(document).ready(function () { // your code here // });
将解决问题。最佳做法是,始终将脚本放在文档底部。
I believe that the above answers is all correct, but that doesn't point out why the
submit
method doesn't work.Well, the
submit
method will not work if jQuery can't get theform
element, and jQuery doesn't give any error about that. If your script is placed in the head of the document, make sure the code runs afterDOM
is ready. So,$(document).ready(function () { // your code here // });
will solve the problem.The best practice is, always put your script in the bottom of the document.
如果您稍后添加了动态表单,您也可以尝试此代码。例如,您使用 ajax 异步加载了一个窗口并想要提交此表单。
情况1,静态版本:如果您只有几个侦听器并且要处理的表单是硬编码的,那么您可以直接在“文档级别”侦听。我不会在文档级别使用侦听器,但我会尝试更深入地了解厄运树,因为它可能会导致性能问题(取决于您网站的大小和内容)
或
或
或
You can also try this code, if you have for example later added dynamic forms. For example you loaded a window async with ajax and want to submit this form.
Case 1, static version: If you have only a few listeners and your form to handle is hardcoded, then you can listen directly on "document level". I wouldn't use the listeners on document level but I would try to go deeper in the doom tree because it could lead to performance issues (depends on the size of your website and your content)
OR
OR
OR
你的代码很好,只是你需要将它放在准备好的函数中。
Your Code is Fine just you need to place it inside the ready function.
你好寻求一种解决方案,使 Ajax 表单与 Google 跟踪代码管理器 (GTM) 配合使用,返回 false 阻止完成并在 google Analytics 上实时提交事件的激活,解决方案是通过 e.preventDefault () 更改返回 false ;正确运行的代码如下:
Hello sought a solution to make an Ajax form work with Google Tag Manager (GTM), the return false prevented the completion and submit the activation of the event in real time on google analytics solution was to change the return false by e.preventDefault (); that worked correctly follows the code:
仅当您的 javascript 没有问题时,e.preventDefault() 才能正常工作,
如果 e.preventDefault() 不起作用,请检查你的 javascripts 很可能你的 JS 的其他部分也不起作用
e.preventDefault() works fine only if you dont have problem on your javascripts,
check your javascripts if e.preventDefault() doesn't work chances are some other parts of your JS doesn't work also
好吧,我遇到了类似的问题。对我来说,问题是 JS 文件在 DOM 渲染发生之前加载。因此,将
移至
标记的末尾。
或使用延迟。
所以请放心,
e.preventDefault()
应该可以工作。Well I encountered a similar problem. The problem for me is that the JS file get loaded before the DOM render happens. So move your
<script>
to the end of<body>
tag.or use defer.
so rest assured
e.preventDefault()
should work.只需定义您的按钮类型,这将阻止表单刷新网页
Just define your button type and this will prevent the form to refresh the webpage
为我工作:
Worked for me: