简体   繁体   English

如何在第一个函数执行时阻止第二个 onclick 函数发生?

[英]How do I stop the second onclick function from happening while the first function is executing?

I'm currently working on a validation feature.我目前正在研究验证功能。 If the form fails to validate, it won't be able to submit, hence, I need to figure out a way to prevent the submitFilloutform() from happening.如果表单验证失败,它将无法提交,因此,我需要找到一种方法来防止 submitFilloutform() 发生。

<button 
  id="submit" 
  name="submit"
  type="submit"
  class="btn btn-warning"
  style="float: right;"
  onclick="validateForm(), submitFilloutform()"
>Submit</button>

Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks!谢谢!

EDIT: I came back to this as someone upvoted it (thanks).编辑:当有人赞成它时,我又回到了这里(谢谢)。 One consideration to add it that inline JS may be a bad idea in the case that you want to lock down potential for cross-site-scripting attacks.添加它的一个考虑因素是,在您想要锁定跨站点脚本攻击的可能性的情况下,内联 JS 可能不是一个好主意。 Part of the defence of that is CSP headers and one strong contender to set is a rule that bans all inline JS.对此的部分防御是 CSP 标头,要设置的一个强有力的竞争者是禁止所有内联 JS 的规则。 So, if you plan to use CSP you might want to move the JS below into an onclick listener for id=submit in a separate JS file.因此,如果您打算使用 CSP,您可能希望将下面的 JS 移动到一个单独的 JS 文件中的 id=submit 的 onclick 侦听器中。

Original answer.原答案。

onClick is executed like a function, so can include JS logic. onClick 像函数一样执行,因此可以包含 JS 逻辑。 Such as...如...

<button 
  id="submit" 
  name="submit" 
  type="submit"  
  class="btn btn-warning" 
  style="float: right;" 
  onclick="if (validateForm()){ submitFilloutform();}"
>
Submit
</button>

This assumed that validateForm() returns a boolean value, of course.当然,这假设 validateForm() 返回一个布尔值。

Why not invoke the second function at the end block inside the first function?为什么不在第一个函数内的结束块调用第二个函数?

<button 
  id="submit"
  name="submit"
  type="submit"
  class="btn btn-warning"
  style="float: right;"
  onclick="validateForm()"
>Submit</button>

function validateForm() {
  // xxx
  submitFilloutform()
}

function submitFilloutform() {
  // xxx
}

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

相关问题 停止发生onClick功能 - Stop an onClick function happening 如果再次调用该函数,如何停止执行该函数的第一次调用? - How do I stop the first call of a function from executing if the function is called again? 如何停止执行功能? - How can I stop a function from executing? Javascript onclick 函数从我第二次点击但不是第一次触发 - Javascript onclick function triggers from the second time i click but not the first Javascript的object.onClick运行函数而不是设置onClick,如何防止这种情况发生,仅执行onClick函数? - Javascript's object.onClick runs the function instead of setting onClick, how can I prevent that from happening and only execute the function onClick? 如何在调整大小时停止onResize函数执行其内容 - How do I stop onResize function executing its contents while resizing 如何使用 onclick function 停止 gsap.to animation? - How do I stop a gsap.to animation with an onclick function? 如何使用一些onclick调用功能按钮停止此循环? - How do I stop this loop with some onclick call function button? 如何阻止onclick,onblur,on *的发生 - How to stop onclick, onblur, on* from happening onClick function 在渲染时被调用——我怎样才能阻止这种情况的发生? - onClick function is called on render -- how can I stop this from occurring?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM