[英]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.