gpt4 book ai didi

javascript - Ajaxcall 不发送输入(MongoDB 数据库)

转载 作者:行者123 更新时间:2023-12-03 04:19:54 26 4
gpt4 key购买 nike

我目前正在使用 Phaser.js 开发一个项目,在处理 Highscore 时遇到了问题。当游戏进入称为“结束”的最后一个状态时,它会打开一个引导模态对话框,其中包含已达到的分数和一个可以输入您的名字的输入。当我点击“发送”时,它应该将两个输入的值放入ajaxcall中并将其发送到“/”。但输入最终是空的,console.log(input1 + "and "+ input2);除了“和”之外什么也没有带出来。我不知道问题是什么,因为我没有收到任何错误。如有任何帮助,我们将不胜感激。

索引.ejs:

<div class="col-md-12" id="popup">
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">

<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<label for="score">Your Score:</label>
<input value="" class="form-control" id="score"><br>
<label for="name">Your Name:</label>
<input value="" class="form-control" id="name" placeholder="Choose a name for the leaderboards...">
</div>
<div class="modal-footer">
<button type="button" id="send" class="btn btn-success">Send</button>
<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
</div>
</div>

</div>
</div>
</div>

ajaxcalls.js

var input1 = $('#name').val();
var input2 = $('#score').val();



$('#send').click(function(){
console.log(input1 + 'x' + input2);
$.ajax({
method: "POST",
url: "/",
data: {
Name: input1,
HScore: input2
},
success: function(data){
console.log('success');
$('#myModal').modal('hide');
}
});
});

index.js

router.post('/', function (req, res) {
var Name = req.body.Name;
var HScore = req.body.HScore;

mongoose.model('hs').create(
{
player: Name,
score: HScore
},
function (err,player) {
if(err){
res.send('Errortext!');
}
console.log('POST creating new Player: ' + player);
res.redirect('/');
});
});

mongo.js

var mongoose = require('mongoose');

var highScore = new mongoose.Schema({
player: String,
score: Number
});

mongoose.model('hs', highScore);

最佳答案

您只需要读取click监听器内的值

$('#send').click(function(){
var input1 = $('#name').val();
var input2 = $('#score').val();

console.log(input1 + 'x' + input2);
$.ajax({
method: "POST",
url: "/",
data: {
Name: input1,
HScore: input2
},
success: function(data){
console.log('success');
$('#myModal').modal('hide');
}
});
});

关于javascript - Ajaxcall 不发送输入(MongoDB 数据库),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43994461/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com