Reputation: 37
I am using MVC4 ,jquery,ajax and json,I have task
In registration form ,fetch the data already entered the data for editing.I edited the content.Then I click on cancel button ,the form data is reset ed to before edited data.
Is any way to avoid the db call to reset the values.? I heard jquery maintaining a history data options for resetting values.
Upvotes: 1
Views: 163
Reputation: 22007
I believe you could have a generic method to save the current value (and other attributes, like checked
) of several inputs, and another to reverse the value to that saved value, using jQuery data
:
function save() {
$("input, textarea, select").each(function() {
$(this).data("savedVal", $(this).val());
$(this).data("savedChecked", $(this).prop("checked"));
});
}
function reset() {
$("input, textarea, select").each(function() {
$(this).val($(this).data("savedVal"));
$(this).prop("checked", $(this).data("savedChecked"));
});
}
Demo. I'd suggest constraining your selector to a specific form though, instead of capturing all inputs in the whole page.
Upvotes: 0
Reputation: 1445
The first time you make a DB call for obtaining the values..... store these values as JSON objects in the client side. When the user edits the data these objects will be able to maintain their state and hence will remain unchanged. Hence on clicking cancel fetch the original values from the JSON objects stored in the client side. Thus in this case the entire flow would be in the client side and no server call would be made.
Upvotes: 1