javascript - 从一个函数中获取值并将其放入另一个函数
问题描述
我需要将函数中inputScore
当前保存的值获取extractVariable
到assignValue
函数中(此函数将值提供inputScore
给 Index.html 页面)。我试图存储inputScore
在一个全局对象中来解决范围问题,但里面assignValue
的global.scoreValue
变量是未定义的。请问如何将 inputScore 输入到 assignValue 中?编程新手 - 任何帮助表示赞赏。谢谢。
global = {};
function extractVariable(inputScore) {
global['scoreValue'] = inputScore;
};
function assignValue() {
document.getElementById("inputScore").value = global.scoreValue;
};
感谢大家的帮助。我非常接近解决我需要做的事情。问题似乎正在inputScore
进入Index.html
页面。我应该首先发布我的所有代码。道歉。index.html
是一个单独的文件,其中包含指向 javascript 文件 ( Game.js
) 的链接。我已经测试了链接并且它正在工作。当在 中按下按钮时Game.js
,index.html
加载、读取 中的assignValue
函数Game.js
,并将玩家得分 ( inputScore
) 插入到表单中的输入值中。目前插入表格的所有内容是:
我无法弄清楚为什么它不起作用。我已经包含了下面两个文件中的代码。再次感谢任何帮助。
Game.js 代码:
function extractVariable(inputScore) {
return inputScore;
};
function assignValue(inputScore) {
document.getElementById("playerScore").value = inputScore;
};
var CrystalRunner = CrystalRunner || {};
CrystalRunner.GameState = {
init: function() {
//...code here
},
create: function() {
//...code here
},
update: function() {
//..code here
//check if the player needs to die
if(this.player.top >= this.game.world.height) {
this.gameOver();
}
},
gameOver: function(){
//..code here
this.updateHighscore();
//..code here
},
updateHighscore: function(){
this.highScore = +localStorage.getItem('highScore');
if(this.highScore < this.myScore){
this.highScore = this.myScore;
this.inputScore = this.highScore;
this.submitScoreButton = this.game.add.sprite(this.game.world.centerX-135, this.game.world.centerY+100, 'submitScoreButton');
this.submitScoreButton.events.onInputUp.add(function() {
window.location.href = "index1.php";
}, this);
extractVariable(this.inputScore);
}
localStorage.setItem('highScore', this.highScore);
},
};
索引.html 代码:
<?php
require_once 'dbconnect.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Crystal Candy Game Login</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css"/>
<link href="css/style.css" rel="stylesheet">
</head>
<body onload="assignValue(extractVariable())" class="bg">
<div id="preloader">
<div id="status"> </div>
</div><!--preloader-->
<div class="wrapper">
<div id="main">
<!-- Form -->
<form id="form-style" method="post" action="crystalhandle.php" autocomplete="off">
<div class="form-group">
<label class="header-text"><span>First Name</span></label>
<input class="form-control" type="text" id="name" name="username" placeholder="Name" title="Please enter your Firstname" required="">
</div>
<div class="form-group">
<label class="header-text"><span>Score</span></label>
<input class="form-control" type="tel" id="playerScore" name="score" value="" readonly>
</div>
<div class="w3ls-btn form-group">
<div class="wthreesubmitaits">
<input type="submit" name="signup" id="reg" class="button" id="next1" value="Send" style="font-family: sans-serif; font-size: 17px; font-weight: bold;"
</div>
</div>
</form>
</div>
</div>
<div id="bodytext"></div>
<script type='text/javascript' src='js/jquery-2.2.3.min.js'></script>
<script type="text/javascript" src="js/phaser.min.js"></script>
<script type="text/javascript" src="js/states/Game.js"></script>
<script>
$(window).on('load', function() {
$('#status').fadeOut();
$('#preloader').delay(350).fadeOut('slow');
$('body').delay(350).css({'overflow':'visible'});
})
</script>
</body>
</html>
我已经做了以下更改,我认为解决方案就在眼前。在Index.html
我通过删除更改以下行inputScore
:
<body onload="assignValue(extractVariable())" class="bg" >
在Game.js
我发现如果我将一个值硬编码到extractVariable
函数中(见下文),硬编码的值将传递到我想要它执行的标记的value
属性中。但是,我仍然无法弄清楚为什么这只适用于硬编码值?<input>
Index.html
function extractVariable(inputScore) {
inputScore = 27; //hard coded value works. Why?
return inputScore;
};
function assignValue(inputScore) {
console.log(inputScore); //this console.logs the hard coded value from
//extractVariable like it should do
document.getElementById("playerScore").value = inputScore;
};
解决方案
您可以通过返回值来做到这一点,extractVariable
然后将其作为参数传递给assignValue
:
function extractVariable(inputScore) {
return inputScore;
};
function assignValue(inputScore) {
document.getElementById("inputScore").value = inputScore;
};
assignValue(extractVariable(inputScore));
推荐阅读
- vba - 检查插入点是在 selection.range 的开头还是结尾
- python-3.x - python3和pypy3有什么区别
- java - 如何在数据库重新启动/断开连接时自动重新连接数据库,该数据库在 openshift 中部署为 java jar 应用程序?
- ios - 如何为 iOS 应用集成 eSewa 支付网关?
- php - POST 路线中可选 slug 的可选训练斜线
- c# - LINQ:记录列表多于预期记录
- javascript - 如何在Javascript中一次性过滤进出数组元素
- c - 如何进一步优化矩阵乘法的性能?
- android - 在 Android 的 alertdialog 中设置 listview
- javascript - 如何使用 react-big-calendar 的 slotPropGetter 为特定时间的特定日期着色?