I have stored values from a html-form to localstorage and I get the data back from localstorage to form for editing. Checked checkbox values (MaterialName) are stored to localstorage as a array. Original checkbox values are coming from database via ajax. What do I need to do to get those values in localstorage match values from database, and to be checked again when editing form? Or is it even possible this way?
// localstorage value
{
"FormID": 150,
"CreateDate": "2019-09-17T00:00:00",
"FormFiller": "JOkuMuu",
"CustomerName": "Wsoy",
"CustomerContact": "Masa",
"WorkName": "Lajitelmapakkaus",
"ReadyToDate": "2019-09-19T00:00:00",
"Instructions": "Tarkasta kirjat ",
"Amount": 50,
"MaterialName": "Xpohja,Tarra"
}
This is function to get all materials from database:
function addMaterials(material) {
$MaterialName.append('<input type="checkbox" value="' + material.MaterialName + '">' + material.MaterialName + ' </input>');
}
$.ajax({
type: 'GET',
url: '/api/materials',
dataType: 'json',
success: function (materials) {
$.each(materials, function (i, material) {
addMaterials(material);
});
},
error: function () {
alert: ('Virhe ladattaessa')
}
});
This is function I tried to get checked values to be re-checked:
$(function () {
//bring data from localStorage
var lsdata = JSON.parse(localStorage.getItem('key'));
//put data to form
$('#formid').val(lsdata.FormID);
$('#createdate').val(lsdata.CreateDate);
$('#formfiller').val(lsdata.FormFiller);
$('#customerlist').empty().append(lsdata.CustomerName.split(',').map(c => new Option(c, c)));
$('#contact').val(lsdata.CustomerContact);
$('#worklist').empty().append(lsdata.WorkName.split(',').map(c => new Option(c, c)));
$('#readytodate').val(lsdata.ReadyToDate);
$('#instructions').val(lsdata.Instructions);
$('#amount').val(lsdata.Amount);
$('#materiallist').append('<input type="checkbox" checked>' + lsdata.MaterialName + ' </input>');
Everything else is working except MaterialName. It is adding new value with Xpohja,Tarra. I want it to add 'checked' to those Xpohja and Tarra, not new value.
Aucun commentaire:
Enregistrer un commentaire