如何在html表单重置jQuery之后执行代码? [英] How to execute code after html form reset with jquery?

查看:84
本文介绍了如何在html表单重置jQuery之后执行代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在点击一个html重置按钮后,

 < input type =reset/> 

我想执行一些代码。我怎么能这样做,并确保表单被重置之前这样做?

解决方案

使用setTimeout作为本在这里做的是最好: https://stackoverflow.com/a/21641295/144665

  $(input [type ='text'])。val('Hello Everybody!'); $'$'
$ b $(input [type ='reset'])。closest('form')。on('reset',function(event){

//在重置表单之前执行
console.log('reset:'+ $(input [type ='text'])。val());
$ b $ setTimeout(函数(){
//在窗体重置后执行
console.log('reset:'+ $(input [type ='text'])。val());
},1);

});

您可能希望将这种形式选择器缩小到所涉及的特定形式,可能带有一个id。 / p>

小提琴证明: http://jsfiddle.net/ iambriansreed / Zh5cd /

After clicking an html reset button,

<input type="reset" />

I would like to execute some code. How can I do this and ensure that the form was reset prior to doing so?

解决方案

Using a setTimeout as Ben does here is best: https://stackoverflow.com/a/21641295/144665

$("input[type='text']").val('Hello Everybody!');

$("input[type='reset']").closest('form').on('reset', function(event) {

  // executes before the form has been reset
  console.log('before reset: ' + $("input[type='text']").val());

  setTimeout(function() {
    // executes after the form has been reset
    console.log('after reset: ' + $("input[type='text']").val());
  }, 1);

});

You might want to narrow that form selector down to the specific form involved, maybe with an id.

Fiddle Proof: http://jsfiddle.net/iambriansreed/Zh5cd/

这篇关于如何在html表单重置jQuery之后执行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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