从JavaScript文件而不是HTML/CSS检索可变值
如果这是一个简单的问题,我深表歉意,但是我花了很长时间被完全卡住,所有指南都从HTML文件中检索,因此我不知道从哪里开始或搜索什么。
<form class="container" method="post">
<div class="form-group">
<input class="form-control" name="title">
</div>
<textarea class="form-control" name="content"></textarea>
<button>add to mongodb</button>
</form>
从HTML文件中的此代码中,我可以检索输入的值,
app.post("/", function(req, res) {
let newGameData = new gameData ({
score: req.body.title,
jumps: req.body.content,
});
newGameData.save();
res.redirect('/')
})
但是,我想从HTML中检索变量,而不是从HTML内部检索变量,而是
var score = [];
var jumps = [];
将包含下面这些变量的JavaScript文件应用于这些变量。得分提高,随着角色的跳跃,在每个生命的结束时,我想将其附加到得分上,并定期检索这些数据以上传到mongodb,反之亦然,我检索并显示高分。
我当时想将可以上传到MongoDB的JSON文件写信,尽管我不知道该如何处理,或者如果它完全有效,那么任何帮助都将不胜感激,谢谢!
I apologize if this is a simple issue but I've spent a long time being completely stuck, all guides are retrieving from the HTML file, and as such I have no clue where to even begin or what to search.
<form class="container" method="post">
<div class="form-group">
<input class="form-control" name="title">
</div>
<textarea class="form-control" name="content"></textarea>
<button>add to mongodb</button>
</form>
From this code in the HTML file, I can retrieve the values inputted,
app.post("/", function(req, res) {
let newGameData = new gameData ({
score: req.body.title,
jumps: req.body.content,
});
newGameData.save();
res.redirect('/')
})
However, instead of retrieving values from HTML forms, I want to retrieve variables from a game inside the HTML, the javascript file containing the below
var score = [];
var jumps = [];
These variables are appended to as the score increases and as the character jumps, at the end of each life, I want to append it to the score, and periodically retrieve this data to be uploaded onto MongoDB, and vice versa I retrieve and display the highscore.
I was thinking to write to a json file that could be uploaded onto MongoDB, though I have no clue how to go about it or if it's valid at all, any help would be greatly appreciated, thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如评论中所建议的,了解Ajax。这是一个示例代码:
在服务器端,不要重定向,而是res.send(结果)。
As suggested in the comments, learn about Ajax. This is a sample code:
and on the server side, don't redirect, but rather res.send(result).
您需要在按钮标签中添加
type = submit
:You need to add
type=submit
in your button tag: