Similar to jQuery how to load some json records into a form fields?, I’m having a rather complex form where fields interact with each other, requiring recalculations (some complex mathematics) as well as disabling fields upon certain conditions. Class at server which may output JSON or similar.
The calc-request is fired by jquery on any field-onchange, and should pick up the class-response and act accordingly. About acting upon the ajax-response:
-
Can the response be formatted so the fields are updated/faded without iterating the JSON-result?
-
Please provide an example of a state-of-the-art json-response for such functionality.
I’m trying not to reinvent the wheel but keen to go as vanilla as possible, plugins tend to break.
EDIT:
In essence:
$.ajax({
url: "theCalcClass.php",
context: document.body
}).done(function() {
//use the json data to update fields w/o foreach...
});
I’m not sure I understand your question. If you are looking for a built-in one-liner like
form.repopulateWith(json), it doesn’t exist. Your options are:Looping the JSON and repopulating each field, or
Setting the field values one by one without looping.
Any other solution (like a plugin) will have to use one of these approaches, there is no other way.