【发布时间】:2018-02-11 10:14:03
【问题描述】:
我正在尝试将表单数据作为 json 数组附加到 localstorage 中,但它不起作用。
这是我的表格
<form>
<label>Choose a option</label>
<input type="radio" name="icon" value="icon1.png">
<input type="radio" name="icon" value="icon2.png">
<input type="radio" name="icon" value="icon3.png">
<label>Name</label>
<input type="text" name="name" placeholder="Your Name">
<button type="submit" id="save">Save</button>
</form>
这是我要保存的内容。
{
"info": [
{
"name": "John",
"icon": "icon1.png"
},
{
"name": "Daniel",
"icon": "icon2.png"
}
]
}
我的尝试
$("form").submit(function(e) {
e.preventDefault();
// serialize `form`
var values = $(this).serializeArray();
values.push(function(item, index) {
$("#test").append(item.name + " " + item.value + "<br>");
})
localStorage.setItem("jsonFav", JSON.stringify(values))
})
【问题讨论】:
-
你尝试了什么?
-
您能否将其包含在您的帖子中而不是在评论中。
标签: javascript jquery html json local-storage