如何在窗体重置时运行JavaScript代码? [英] How to run JavaScript code when a form is being reset?

查看:133
本文介绍了如何在窗体重置时运行JavaScript代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我们可以附加一个处理程序到 onsubmit ...但是我们怎样才能为表单重置事件添加一个处理程序呢? (通常点击< input type =reset>



或者...是不是这样的事件...所以问题变成如何解决这个问题?



(现在,我想在

后运行一个处理程序 em>重置事件;但有一天我可能需要在重置事件之前运行

解决方案

根据 MDN < ; form> 标签支持 onreset 事件。

Onreset 在实际重置表单之前触发;重置后似乎没有任何事件发生。我测试了一下,看看重置是否会为值重置的输入触发 onchange 事件,但它看起来不是。



重置后做某些事情的解决方法可能是在重置时设置一个标志,然后使用重置按钮的 onblur 事件(所以在重置后,它会在你下次点击其他东西时运行)。当然,另一种解决方法是触发setTimeout,以便在重置后脚本运行很短的时间。任何一个都有点骇人,我担心。

I know we can attach a handler to the form onsubmit... But how can we add a handler to the form reset event? (usually when clicking on <input type="reset">)

Or... Maybe there is no such event... So the question becomes on how to work-around that?

(right now, I want to run a handler after the reset event; but someday I might need to run before the reset event)

解决方案

According to MDN, the <form> tag supports an onreset event.

Onreset fires before the actual resetting of the form; there doesn't appear to be any event for after resetting. I tested to see if the reset would fire an onchange event for the inputs whose values are reset, but it does not appear to.

A workaround for doing something after resetting might be to set a flag on reset, and then use the onblur event of the reset button (so after you reset, it would run the next time you click on something else). An alternate workaround, of course, is to trigger a setTimeout so that your script would run a short time after the reset. Either one is a bit of a hack, I'm afraid.

这篇关于如何在窗体重置时运行JavaScript代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆