我正在使用ImpactJS
引擎开发一款游戏,我创建了一个包含输入框和提交按钮的基本表单。我能够从输入框中检索值,但我想要的是,当玩家点击提交输入框中的任何值时,就会获取提示,我应该能够在提交点击时获得该值。如果值为空,我应该得到一个警告,说“没有价值或任何”。我想使用这个最终值,并将其分配给一个变量,我可以在Impact引擎中的JavaScript文件中使用该变量来跟踪游戏中的输入。我对HTML,CSS一般是新手,所以我不知道如何实现这一点。提交按钮时提取输入框值点击
下面是我的HTML/CSS代码,它有一个输入框和一个提交按钮。
<!DOCTYPE html>
<html>
<head>
<title>Impact Game</title>
<style type="text/css">
html,body {
background-color: #333;
color: #fff;
font-family: helvetica, arial, sans-serif;
margin: 0;
padding: 0;
font-size: 12pt;
}
#problemform {
display: none;
width: 300px;
height: 100px;
}
#probleminput {
position: absolute;
display: none;
top: 450px;
left: 240px;
height: 50px;
width: 350px;
}
#problemsubmit {
position: absolute;
display: none;
top: 530px;
left: 623px;
height: 40px;
width: 100px;
padding: 5px 10px 8px 2px;
}
#prob_submit_msg {
width: 30%;
margin-left: auto;
margin-right: auto;
text-align: center;
}
#canvaswrapper {
position: relative;
height: 768px;
width: 1024px;
display: block;
margin: auto;
margin-top: 80px;
vertical-align: middle;
}
#canvas {
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}
</style>
<script type="text/javascript" src="lib/jquery.min.js"></script>
<script type="text/javascript" src="lib/impact/impact.js"></script>
<script type="text/javascript" src="lib/game/main.js"></script>
</head>
<body>
<div id="canvaswrapper">
<canvas id="canvas" width="1024" height="768"></canvas>
<div id="problemform" class="form-inline">
<input id="probleminput" class="form-inline" type="text" style="display: inline;"></input>
<button id="problemsubmit" class="btn" style="display: inline-block;">Submit</button>
</div>
<div id ="prob_submit_mssg" style="display: block;"></div>
</div>
</body>
</html>
下面是不同的JS文件显示输入框,并使用jQuery
ProblemDisplay:function() {
this.setQuestion('This is a title','this is where the body will go and it will be super long and impossible to read or understand.', 'This is a hint');
this.isActive = true;
var form = $("#problemform");
var inputBox = $("#probleminput");
var submitButton = $("#problemsubmit");
form.show();
inputBox.show();
submitButton.show();
},
这是我的工作,现在提交按钮我的ImpactJS
代码块。但是现在我希望在输入框中传递的字符串在单击提交按钮时存储在另一个变量中。如何实现这一目标?
谢谢!
所以,你只需要有一个事件提交按钮,当提交按钮事件firred得到的值输入?而已? – Pavlo
虽然基本上它更复杂。它就像我们构建的问题答案界面。因此,无论何时玩家在文本字段中输入答案并按下提交,我都必须将该答案存储在某个变量中。我应该能够在我的js文件中使用该变量来检查答案是否与数据库中存储的答案相匹配(mongodb)。这是设计。我不知道我是否在正确的轨道上实现这一目标。 – Htlcs