gpt4 book ai didi

javascript - 如何在 jquery 提交之前执行我的函数,直到验证正确

转载 作者:行者123 更新时间:2023-12-03 09:00:17 24 4
gpt4 key购买 nike

我想在单击按钮时调用JS函数,然后继续执行下面的jquery脚本

表格:

<form id="my_form">
<button type="button" id="Bshift" onclick="return validation()">Clear</button>
</form>

纯JS函数:

function validation(){
// this has to be executed first
// do something here..
}

Jquery:

$(document).ready(function(){
$('#Bshift').click(function(){
// this has to be executed second after everything in the validation is correct.
// do something here that would be to insert to sql for example.
}
});

我想知道在 jquery 提交之前如何执行我的函数。

最佳答案

从按钮标记中删除 onclick 属性并检查单击函数内的验证

  <form id="my_form">
<button type="button" id="Bshift">Clear</button>
</form>

validation() 函数返回 true 或 false

function validation(){

return validationResult; // whether valid i.e. return true or if not valid return false

}

然后

  $('#Bshift').click(function(){
var isValid=false;
isValid = validation();
// this has to be executed second after everything in the validation is correct.
if(isValid)
{
// do something here that would be to insert to sql for example.
}
});

关于javascript - 如何在 jquery 提交之前执行我的函数,直到验证正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32326184/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com