呼叫重置但在FORM之外? [英] Call reset but outside of a FORM?

查看:107
本文介绍了呼叫重置但在FORM之外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆输入,我想要重置,但有一个表单外的所有内容。我试着在输入和textarea上调用reset,但没有运气。



是否有类似的函数可以使用?

解决方案

请记住,RESET表单实际上并不清除所有字段,它也会将表单的默认值重置为默认值,所以更好的方法可能如下: p>

  $('#the_form')。trigger('reset'); 

也许另一种方法:

  //捕获所有现有值

var arr = []; $(b
$ b $(':input')。each b});

//自定义函数将所有值重置为最初捕获的值

函数my_reset()
{
$(':input')。each (函数(i,e)
{
$(e).val(arr [i]);
});

$ / code>

上述方法盲目地针对所有字段,如果您有更好的目标他们,你应该肯定使用你可以。



另外,这种方法按顺序存储所有的字段,所以如果你有动态生成的字段,那么这个解决方案将不得不被修改。

I have a bunch of inputs, i'd like to call reset but there are all outside of a form. I tried calling reset on input and textarea with no luck.

Is there a similar function i can use?

解决方案

Keep in mind that the form RESET actually doesn't clear all fields, it will reset a form's default values back to default as well, so a better approach might be the following:

$('#the_form').trigger('reset');

Perhaps another approach:

// capture all existing values

var arr = [];

$(':input').each(function(i, e)
{
    arr.push($(e).val());
});

// custom function to reset all values to initially captured values

function my_reset()
{
    $(':input').each(function(i, e)
    {
        $(e).val(arr[i]);
    });
}

The above approach blindly targets all fields, if you have a way to better target them, you should definitely use what you can.

Additionally, this approach stores all the fields in order, so if you have dynamically generated fields, then this solution would have to be revised.

这篇关于呼叫重置但在FORM之外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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