简体   繁体   English

Javascript / Jquery:在表单提交完成之前,如何防止执行其他代码

[英]Javascript/Jquery : how we can prevent execution of other code until form submission done

document.myForm.submit();
window.dialogArguments.close();

dialogArguments.close() line is getting executed first and document.myForm.submit() line is getting executed last. dialogArguments.close()行首先执行,而document.myForm.submit()行最后执行。

How can we stop the interpeter to execute other lines of code until substitution done? 在替换完成之前,我们如何停止交织器执行其他代码行?

You should not prevent any part of code. 您不应阻止任何代码部分。 Just need put your code in right place. 只需将您的代码放在正确的位置。

For example: 例如:

function deleteDocument(form) {
    if (confirm("Do you really want to delete the document?")) 
       form.submit();
       window.dialogArguments.close();
   }

<form action="deleteDocument.php" onsubmit="deleteDocument(this);return false;">

</form>

Try like 尝试像

document.myForm.submit();
setTimeout(
    function() {
      window.dialogArguments.close();    },
 500);

Set time out for delay execute for function. 设置超时以执行功能。 or Use as callback function to window.dialogArguments.close() ; 或用作window.dialogArguments.close()的回调函数;

   <form onsubmit="do_something()">

     function do_something(){
      // Do your stuff here
     }

If you put return like the code below, you can prevent the form submission by returning false from the do_something() function. 如果像下面的代码那样放置return,则可以通过从do_something()函数返回false来阻止表单提交。

    <form onsubmit="return do_something()">

     function do_something(){
    // Do your stuff here
    return true; // submit the form

    return false; // don't submit the form
   }

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

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