如何在没有后端的情况下保存用户输入 [英] How to save user input without backend

查看:60
本文介绍了如何在没有后端的情况下保存用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在学习 JavaScript 和一般的 Web 开发,我想知道我想做的事情是否可行.我想编写一个 JavaScript 测验,无需后端即可保存用户输入的答案.这可能吗?

I am just learning JavaScript and web development in general and I was wondering if what I want to do is possible. I would like to write a JavaScript quiz that saves the answers a user inputs without needing a backend. Is this possible?

如果不可能,我能做到的最简单和最简单的方法是什么?

If it is not possible what is the simplest and easiest way I can do this?

谢谢

PS:如果这不是问这个问题的正确地方,请指引我到我应该问这个的地方.

PS: If this is not the right place to ask this, please direct me to the place I should be asking this.

推荐答案

如果您打算只为用户存储它们,您可以使用 localstorage

If you're planning on just storing them for just the user you could use localstorage

有多种方法可以使用它,但这是一个非常简单的版本,应该会为您指明正确的方向.

There are a number of ways to use this, but this is a very simple version that should point you in the right direction.

正如@patick 在下面提到的 - 如果您想存储更完整的数据,您需要对数据进行 JSON.stringify,然后在您准备使用数据时进行解析.

As @patick mentions below - if you wanted to store more complete data you would need to JSON.stringify the data then parse later when you are ready to consume the data.

另请注意,所有 localstorage 都保存为字符串,因此您需要将其设为整数才能真正使用它.

Also note that all localstorage is saved as a string so you would need to make it an integer to really use it.

你可能会这样做

// set inital score
var score = parseFloat(localStorage.getItem('score'));

// if they get the answer correct
score = score + 1;

// update the score
localstorage.setItem('score',score);

// go to next question

这篇关于如何在没有后端的情况下保存用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆