Currently beating my head against a wall wondering why I can't seem to set the checked property of a checkbox based on the the value of a variable.
I'm trying to make a page editable. When the user chooses to edit the page, the current values get stored in sessionStorage and recalled in order to replace any changed values with their original value if the user cancels. I've stored the original checked values (true or false) in session storage and I'm trying to use those values to populate the checked state on cancel. The below code is simplified to only include what I feel is the "problem" bits.
var value = sessionStorage.getItem(key);
console.log(key + ' = ' + value); //produces the correct key/value pair
$('input[name="' + key +'"]').prop('checked') === value;
the checkboxes always revert to whatever the current value is.
Here's what I've also tried:
$('input[name="' + key +'"]').prop('checked', value);
$('input[name="' + key +'"]').prop(':checked', value);
$('input[name="' + key +'"]').attr('checked', value);
and pretty much every other combination I can possibly think of. Any help would be greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire