gpt4 book ai didi

javascript - 如何在jquery按钮点击之前调用JS函数(通过选择器点击)

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

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

表格:

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

纯JS函数:

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

Jquery:

$(document).ready(function(){
$('#Bshift').submit(function(){
// this has to be executed second
// do something here.
}
});

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

最佳答案

HTML:

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

JS:

$(document).ready(function(){
$('#Bshift').on('click', function(){ // I think this should be click event because you're not submitting your page you're just clearing it based on "CLEAR"
validation();
}

function validation(){
do something here..
}
}

注意:您的函数必须位于事件触发器之外。

关于javascript - 如何在jquery按钮点击之前调用JS函数(通过选择器点击),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32324532/

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