I am calling an api from python that returns json to the javascript function. When returning the data I am setting the value of an input field using jquery. But when saving theform the field value is not being saved. What's the solution? Thanks in advance.
$.each(data['payload'], function(key, val) {
let el = $('[name='+key+']');
el.val(val);
});
Sometimes websites use form fields only as "frontend" for some internal state that gets synced whenever the data in the field changes.
When a user changes the contents of the field, a change
event is fired. This won't happen if you programmatically change the value though, so such a change would not be picked up by code listening for changes.
To fix this problem, you can manually trigger a change
event on the field after modifying its value. You seem to be using jQuery, there you can simply use the change()
method like this:
el.val(val).change()
^^^^^^^^^