Reputation: 4160
I have several questions about localStorage forms.
So I have these 3 types of inputs:
Radio
<form id="form54" name="form54" enctype="multipart/form-data" method="post" action="#public">
<!-- select -->
<div>
<select name="Q1">
<option value="0">opt1</option>
<option value="1">opt2</option>
<option value="2">opt3</option>
<option value="other">Other</option>
</select>
<textarea id="Q-option-other-value" class="with-attachments" maxlength="100" placeholder="placeholder" name="Q1-other"></textarea>
</div>
<br><br>
<!-- checkbox -->
<div>
<input id="opt3a" type="checkbox" name="Qthree" value="" store="opt2a">
<label for="opt3a">opt1</label>
<input id="opt3b" type="checkbox" name="Qthree" value="" store="opt2b">
<label for="opt3b">opt2</label>
<input id="opt3c" type="checkbox" name="Qthree" value="other" store="opt2c">
<label for="opt3c">Other</label>
<textarea id="Q3-checkbox-other-value" class="with-attachments" maxlength="150" placeholder="placeholder" name="Q3-other"></textarea>
</div>
<br><br>
<!-- radio -->
<div>
<input id="opt4a" type="radio" name="Qfour" value="1" store="opt4a">
<label for="opt4a" class="label-for-radio">opt1</label>
<input id="opt4b" type="radio" name="Qfour" value="2" store="opt4b">
<label for="opt4b" class="label-for-radio">opt2</label>
<input id="opt4c" type="radio" name="Qfour" value='scheduled' store="opt4c">
<label for="opt4c" class="label-for-radio">other</label>
<input class="absolute" type="date" name="Qfour">
</div>
For each one I have a "special option" that shows a different input if that option is selected.
Plus, I have localStorage, but I can't make it work for checkbox and radio. It only works in textarea and input=select.
I have some scripts in jQuery:
What is missing - here i need your help
Fiddle: http://jsfiddle.net/sandrin4p/ury7dvt5/17/
Upvotes: 0
Views: 540
Reputation: 624
Just setting the values for the select, radio and checkbox inputs won't do the trick. The .val() function just sets the value="" attribute.
if (localStorage.getItem("flag") == "set") {
var data= $("#form54").serializeArray();
$.each(data, function(i, obj) {
console.log(i, obj);
if($("[name='" + obj.name +"']").attr('type') === 'radio' ||
$("[name='" + obj.name +"']").attr('type') === 'checkbox') {
if(obj.name) $("[name='" + obj.name +"']").prop('checked', true);
else $("[name='" + obj.name +"']").prop('checked', false);
} else if( $("[name='" + obj.name +"']").attr('type') === 'select') {
if(obj.name) $("[name='" + obj.name +"'] option[value='" + obj.value +"']").prop('selected', true)
else $("[name='" + obj.name +"'] option[value='" + obj.value +"']").prop('selected', false)
} else
$("[name='" + obj.name +"']").val(localStorage.getItem(obj.name) );
}
});
}
should do the trick. (sorry for any typos just did this quickly (and i dont know if your object has a .value prop.
But i think you get the idea.
Good luck
Upvotes: 1