表单重置后调用一个函数 [英] Call a function after form reset

查看:290
本文介绍了表单重置后调用一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法让我在单击窗体中的重置按钮后调用一个函数,我的意思是之后,以便表单首先重置,然后调用我的函数。正常事件冒泡会调用我的函数,然后才重置表单。现在我想避免setTimeout来做到这一点。



我需要的是在表单重置时调用一个函数,因为当值更改时,我需要更新统一且统一的需求。



目前我这样做:
$ b

// Reset ('button'[type ='reset'])。live('click',function(){
elem = this;
/ /可悲的是,我们需要使用setTimeout在重置发生后执行此操作
setTimeout(function(){
$ .each($(elem).parents('form')。find(输入),函数(){
$ .uniform.update($(this));
});
},50);
});

我试着在 $(':input')上做这个。 change()但重置元素似乎不会触发change事件。

提前感谢您的帮助。

解决方案

HTML表单确实有一个onReset事件,您可以在其中添加您的调用:

 函数updateForm()
{
$ .each($('form')。find(:input),function() {
$ .uniform.update($(this));
});
}

< form onReset =updateForm();>

正如FrédéricHamidi ,您也可以使用 bind 如下:

<$ p $($(this).find(:input),function()$($'code> $('form')。 {
$ .uniform.update($(this));
});
});

经过一些测试后,在复位发生之前两种方式都出现了,而不是之后。你现在这样做的方式似乎是最好的方式。



同样的结论发现在问题这里


Is there a method for me to call a function after click on the reset button in form, and I mean after, so that the form is first reset and then my function called. Normal event bubbling would call my function and only then reset the form. Now I would like to avoid setTimeout in order to do this.

What I need is to call a function when a form is reset because I use uniform and uniform needs to be updated when values change.

At the moment I do it like this:

//Reset inputs in a form when reset button is hit  
$("button[type='reset']").live('click', function(){  
    elem = this;  
    //Sadly we need to use setTimeout to execute this after the reset has taken place  
    setTimeout(function(){  
        $.each($(elem).parents('form').find(":input"), function(){  
            $.uniform.update($(this));  
        });  
    }, 50);  
});  

I tried to do al this on $(':input').change() but reseting an element does not seem to trigger the change event.
Thank you in advance for any help.

解决方案

HTML forms do have an onReset event, you can add your call inside there:

function updateForm()
{
    $.each($('form').find(":input"), function(){  
        $.uniform.update($(this));  
    });  
}

<form onReset="updateForm();">

As pointed out in the comment by Frédéric Hamidi you can also use bind like so:

$('form').bind('reset', function() {
    $.each($(this).find(":input"), function(){  
        $.uniform.update($(this));  
    }); 
});

After some testing it appears both ways fire before the reset takes place and not after. The way your doing it now appears to be the best way.

The same conclusion was found in this question here

这篇关于表单重置后调用一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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