防止表单提交上的默认 jQuery

发布于 2024-11-17 02:02:27 字数 568 浏览 3 评论 0原文

这有什么问题吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

玩心态 2024-11-24 02:02:27

试试这个:

$("#cpa-form").submit(function(e){
    return false;
});

Try this:

$("#cpa-form").submit(function(e){
    return false;
});
影子是时光的心 2024-11-24 02:02:27

使用新的“on”事件语法。

$(document).ready(function() {
  $('form').on('submit', function(e){
    // validation code here
    if(!valid) {
      e.preventDefault();
    }
  });
});

引用:https://api.jquery.com/on/

Use the new "on" event syntax.

$(document).ready(function() {
  $('form').on('submit', function(e){
    // validation code here
    if(!valid) {
      e.preventDefault();
    }
  });
});

Cite: https://api.jquery.com/on/

我不咬妳我踢妳 2024-11-24 02:02:27

这是一个古老的问题,但这里接受的答案并没有真正触及问题的根源。

您可以通过两种方式解决这个问题。首先使用 jQuery:

$(document).ready( function() { // Wait until document is fully parsed
  $("#cpa-form").on('submit', function(e){

     e.preventDefault();

  });
})

或者不使用 jQuery:

// Gets a reference to the form element
var form = document.getElementById('cpa-form');

// Adds a listener for the "submit" event.
form.addEventListener('submit', function(e) {

  e.preventDefault();

});

您不需要使用 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:

$(document).ready( function() { // Wait until document is fully parsed
  $("#cpa-form").on('submit', function(e){

     e.preventDefault();

  });
})

Or without jQuery:

// Gets a reference to the form element
var form = document.getElementById('cpa-form');

// Adds a listener for the "submit" event.
form.addEventListener('submit', function(e) {

  e.preventDefault();

});

You don't need to use return false to solve this problem.

不甘平庸 2024-11-24 02:02:27

我相信上面的答案都是正确的,但这并没有指出为什么 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 the form 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 after DOM 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.

稍尽春風 2024-11-24 02:02:27
$('#cpa-form input[name="Next"]').on('click', function(e){
    e.preventDefault();
});
$('#cpa-form input[name="Next"]').on('click', function(e){
    e.preventDefault();
});
生死何惧 2024-11-24 02:02:27

已弃用 - 此部分已过时,因此请不要使用它。

如果您稍后添加了动态表单,您也可以尝试此代码。例如,您使用 ajax 异步加载了一个窗口并想要提交此表单。

$('#cpa-form').live('submit' ,function(e){
    e.preventDefault();      
    // do something
});

更新 - 如果您想处理动态添加的内容,您应该使用 jQuery on() 方法尝试监听文档 DOM。

情况1,静态版本:如果您只有几个侦听器并且要处理的表单是硬编码的,那么您可以直接在“文档级别”侦听。我不会在文档级别使用侦听器,但我会尝试更深入地了解厄运树,因为它可能会导致性能问题(取决于您网站的大小和内容)

$('form#formToHandle').on('submit'... 

$('form#formToHandle').submit(function(e) {
    e.preventDefault();      
    // do something
});

情况 2,动态版本:如果您已经在代码中收听了文档,那么这种方式对您来说会有好处。这也适用于稍后通过 DOM 添加或使用 AJAX 动态添加的代码。

$(document).on('submit','form#formToHandle',function(){
   // do something like e.preventDefault(); 
});

$(document).ready(function() {
    console.log( "Ready, Document loaded!" );

    // all your other code listening to the document to load 

    $("#formToHandle").on("submit", function(){
        // do something           
    })
});

$(function() { // <- this is shorthand version
   console.log( "Ready, Document loaded!" );

    // all your other code listening to the document to load 

    $("#formToHandle").on("submit", function(){
        // do something           
    })
});

DEPRECATED - this part is outdated so please don't use it.

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.

$('#cpa-form').live('submit' ,function(e){
    e.preventDefault();      
    // do something
});

UPDATE - you should use the jQuery on() method an try to listen to the document DOM if you want to handle dynamically added content.

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)

$('form#formToHandle').on('submit'... 

OR

$('form#formToHandle').submit(function(e) {
    e.preventDefault();      
    // do something
});

Case 2, dynamic version: If you already listen to the document in your code, then this way would be good for you. This will also work for code that was added later via DOM or dynamic with AJAX.

$(document).on('submit','form#formToHandle',function(){
   // do something like e.preventDefault(); 
});

OR

$(document).ready(function() {
    console.log( "Ready, Document loaded!" );

    // all your other code listening to the document to load 

    $("#formToHandle").on("submit", function(){
        // do something           
    })
});

OR

$(function() { // <- this is shorthand version
   console.log( "Ready, Document loaded!" );

    // all your other code listening to the document to load 

    $("#formToHandle").on("submit", function(){
        // do something           
    })
});
南笙 2024-11-24 02:02:27
$(document).ready(function(){
    $("#form_id").submit(function(){
        return condition;
    });
});
$(document).ready(function(){
    $("#form_id").submit(function(){
        return condition;
    });
});
花开浅夏 2024-11-24 02:02:27

你的代码很好,只是你需要将它放在准备好的函数中。

$(document).ready( function() {
  $("#cpa-form").submit(function(e){
     e.preventDefault();
  });
}

Your Code is Fine just you need to place it inside the ready function.

$(document).ready( function() {
  $("#cpa-form").submit(function(e){
     e.preventDefault();
  });
}
囚我心虐我身 2024-11-24 02:02:27

你好寻求一种解决方案,使 Ajax 表单与 Google 跟踪代码管理器 (GTM) 配合使用,返回 false 阻止完成并在 google Analytics 上实时提交事件的激活,解决方案是通过 e.preventDefault () 更改返回 false ;正确运行的代码如下:

 $("#Contact-Form").submit(function(e) {
    e.preventDefault();
   ...
});

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:

 $("#Contact-Form").submit(function(e) {
    e.preventDefault();
   ...
});
二智少女 2024-11-24 02:02:27

仅当您的 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

凉城凉梦凉人心 2024-11-24 02:02:27

好吧,我遇到了类似的问题。对我来说,问题是 JS 文件在 DOM 渲染发生之前加载。因此,将

或使用延迟。

<script defer src="">

所以请放心,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.

<script defer src="">

so rest assured e.preventDefault() should work.

为人所爱 2024-11-24 02:02:27

只需定义您的按钮类型,这将阻止表单刷新网页

<button type="button" id="saveBtn">Save</button>
 $('#saveBtn').click(function() {

 });

Just define your button type and this will prevent the form to refresh the webpage

<button type="button" id="saveBtn">Save</button>
 $('#saveBtn').click(function() {

 });
薯片软お妹 2024-11-24 02:02:27

为我工作:

     $("#submitButtonOnForm").click(function(e) {            
        if (somecondition) {
            e.preventDefault();
            console.log('not submitted');
        }
        //form submission continues, no code needed
     });

Worked for me:

     $("#submitButtonOnForm").click(function(e) {            
        if (somecondition) {
            e.preventDefault();
            console.log('not submitted');
        }
        //form submission continues, no code needed
     });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文