How to reset form status with model data?

Asked

Viewed 384 times

0

I have a model in which fills the fields of a form (use Asp.net mvc and the view is typed), that is, I load the fields of this form according to the data of the database for the user to change in the editing screen.

What I want to do is a "undo" button that resets to the initial state of the data that came from the database, not simply using this.meuform.reset() at the click of the button.

I believe that it should be done in the form onload() a way to save the current elements of the form and when clicked on the button, simply play again in the elements, these saved elements...

I prefer pure Javascript.

  • Dude, it’s not clear to me what you want to do. The reset() in itself already reset the value for the initial state, that is, return its value to what is in the attribute value, that was set initially. Now, did not understand your last paragraph.

  • is that the reset it "Zera" all fields, puts "" in all the right values? I want to leave the fields with the values of the model, for example: I search in the bank the name john and fill in the input value with this name, then I change this name again.. after that, I want to click and go back to the amount that came back from the bank that was "João".

  • 2

    I don’t think so, man reset does not clear the values but resets them even to the initial value, take a look at in this example. As far as I know, Html.EditorFor or similar that you may be using, generates the value in the attribute value, which is the correct standard. So the reset was supposed to work.

  • It really makes sense, I will test again, I may have made some mistake, warning here then, if solve, can post as response.

  • @Dontvotemedown I believe that it is really working, but I would like to take the form without having to set a name or ID for it, because it would have to be set in all the Foms of all pages, is there any way Generic to pick up? ? Document.getelementsbytagname("form"). reset();

  • No, this type of writing works in jQuery because it runs a loop internally. The getElementsByTagName() returns a collection that has no method reset(). You’d have to run the loop yourself, example

  • have how to post as answer to both questions? Can be in jquery.

  • @Dontvotemedown look at the comment above, if you can, wait. Thanks for the help.

  • Whoa, buddy, you got it.

Show 4 more comments

1 answer

1


The reset() as explained in the documentation...

The Htmlformelement.reset() method restores a form element’s default values

should yes reset the value of the elements to the original state, and not clean them, leaving blank or things like that, as in example.

What can be done in your case - which has multiple Forms - select them and in a loop reset to all:

var forms = document.getElementsByTagName("form");
// ou
// var forms = document.querySelectorAll("form");

for (var i = 0; i < forms.length; i++)
{
    forms[i].reset();
}

Or with jQuery:

var forms = $("form");

for (var i = 0; i < forms.length; i++)
{
    forms[i].reset();
}

Demos 1, 2 or with more elements.

UPDATE:

To catch only the first (or only) form of the screen:

var forms = document.getElementsByTagName("form");
// ou
// var forms = document.querySelectorAll("form");

if (forms.length > 0)
{
    forms[0].reset();
}

That way you avoid possible exceptions if the routine tries to reset a form that does not exist.

  • I only have one per view, I mean, I wouldn’t need the 'for', but this "form" needs to be the form id itself right? There is no way to catch without entering id by id for example?

  • 1

    The form is the tag, not the id. It will catch all the <form> document, but if you only have one, just directly access document.getElementsByTagName("form")[0].reset().

  • 1

    @Luiznegrini Check out the latest update.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.