在重置按钮处理单击后执行 Javascript 操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6739191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:41:39  来源:igfitidea点击:

Perform Javascript action after reset button handles click

javascripthtmlonclickreset

提问by user541686

How do I perform an action immediately afteran <input type="reset"/>has already reset the form elements?

如何立即执行一个动作一个<input type="reset"/>已重置表单元素呢?

回答by ChristopheCVB

Try :

尝试 :

<input type="reset" onclick="return resetForm();"/>

function resetForm(){
    setTimeout(function(){
        // do whatever
    }, 50);
    return true;
}

回答by digitalbath

Forms have a resetevent that you can listen for.

表单有一个reset您可以监听的事件。

<script>
function resetHandler() {
    // code
}
</script>
<form ... onreset="resetHandler();">
</form>

Of course, it's bad practice to add javascript handlers this way, so you'd want to use .addEventListener/.attachEventor jQuery.bind(), but you get the idea.

当然,以这种方式添加 javascript 处理程序是不好的做法,因此您想使用.addEventListener/.attachEventor jQuery.bind(),但您明白了。

回答by Somnath Muluk

Write code/events which you wanted to call in middle of this function. I have tested this. Working good.

编写您想在此函数中间调用的代码/事件。我已经测试过了。工作良好。

$(document).ready(function() {
    $("input:reset").click(function() {       // apply to reset button's click event
        this.form.reset();                    // reset the form

        // call your functions to be executed after the reset      

         return false;                         // prevent reset button from resetting again
    });
});

回答by caarlos0

You always can use jQuery, or do some tricks with form reset event itself.

你总是可以使用 jQuery,或者用表单重置事件本身做一些技巧。