我在 html 中创建了一个表单,我想将用户插入的数据以 json 对象和数组的形式存储在 JSON 文件中,我是 json 和 javascript 的新手,所以请告诉我如何做到这一点?
这是我的 html 表单:
<!DOCTYPE html>
<html>
<body>
<form>
First name:<input type="text" name="firstname">Last name:<input type="text" name="lastname"><br>
Gender: <input type="radio" name="gender">male<input type="radio" name="gender">female<br>
Married: <input type="checkbox" name="married">yes<input type="checkbox" name="merried">no<br>
Degree:
<select>
<option>BS</option>
<option>MS</option>
<option>PhD</option>
</select>
Description: <textarea style="width:200px"></textarea>
</form>
</body>
</html>
我想要输出 .json 文件,如下所示:
[
{
"first name": "adam",
"last name": "smith"
"gender": "male",
"merried": "yes"
"degree": "MS",
"description": "any discription"
},
{
"first name": "anglina",
"last name": "jouley"
"gender": "female",
"merried": "no"
"degree": "PhD",
"description": "any discription"
},
]
最佳答案
您不能仅在前端使用 javascript 和 html 将其保存到文件系统。为此,您将需要一个后端。
Steps should be:
- Serialize form data to json using jQuery or plain javascript.
- Send json data through ajax/xmlhttprequest to a backend server.
- Receive json data at server side and save to a .json file
关于javascript - 将表单数据存储到 json 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34366826/