简体   繁体   English

提交表单时未触发 javascript onsubmit 事件

[英]javascript onsubmit event not triggering when form is submitted

 (function ($) { function additionalDetailsForm() { additional_info_flag = true; checkAdditionalInfoInput(); return additional_info_flag; } });
 <form method="POST" onsubmit="return additionalDetailsForm();"></form>

When I submit the form, it is directly going to action.php , and not triggering the onsubmit event.当我提交表单时,它直接进入action.php ,并且不会触发onsubmit事件。

Why is this happening?为什么会这样?

Unwrap your additionalDetailsForm from (function ($) {... like below and then try.(function ($) {...中解开您的additionalDetailsForm ,如下所示,然后尝试。

What is happening is when you wrap function inside (function ($) {... it will have scope limited to that block . And your return additionalDetailsForm(); will try to find additionalDetailsForm function with global scope. So in your case it is not working. What is happening is when you wrap function inside (function ($) {... it will have scope limited to that block . And your return additionalDetailsForm(); will try to find additionalDetailsForm function with global scope. So in your case it is不工作。

 function additionalDetailsForm() { alert(1); additional_info_flag = true; checkAdditionalInfoInput(); return additional_info_flag; }
 <form method="POST" onsubmit="return additionalDetailsForm();"> <button type='submit'>submit</button> </form>

Change the jquery method and others in following way:-通过以下方式更改 jquery 方法和其他方法:-

 $(function() { console.log( "ready;" ). $( "#target" );submit(function( event ) { var additional_info_flag = true; checkAdditionalInfoInput(). console;log(additional_info_flag); return additional_info_flag. event;preventDefault(); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form id="target" action="destination.html"> <input type="text" value="Hello there"> <input type="submit" value="Go"> </form>

There are a few issues:有几个问题:

  1. You're wrapping your function in another, anonymous function.您将 function 包装在另一个匿名 function 中。 That means your function will be defined in the scope of the anonymous function and won't be available globally.这意味着您的 function 将在匿名 function 的 scope 中定义,并且不会在全球范围内可用。 Remove the (function ($) {... }) surrounding your function.删除 function 周围的(function ($) {... })

  2. Your function always returns true , which means the form will always be submitted.您的 function 始终返回true ,这意味着将始终提交表单。 If you don't want that to happen, you want to either return false from that function or prevent the form submission event from triggering.如果您不希望这种情况发生,您可以从 function 返回false或阻止表单提交事件触发。

So your code could be changed to:因此,您的代码可以更改为:

function additionalDetailsForm() {
    let additional_info_flag = false;
    checkAdditionalInfoInput();

    // return false, to prevent the form from being submitted
    return additional_info_flag;
}

Or, if you never want the form to be submitted:或者,如果您不想提交表单:

function additionalDetailsForm(event) {
    // prevent the form from being submitted by preventing
    // the submission event from triggering
    event.preventDefault();

    checkAdditionalInfoInput();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM